0

In an asp.net mvc3 view, I have a form with an input. The input has regex for a number range. The regex is correct, but the validation always comes back as invalid. I am not sure what I am missing here. This is the rendered element. It is appended to the DOM after the page loads at runtime.

<input 
 id="ReserveQuantity" 
 class="text-box single-line" 
 type="text" 
 name="ReserveQuantity" 
 data-val="true" 
 data-val-required="required." 
 data-val-number="numeric" 
 data-val-regex-pattern="/^(?:[0-9]|[1-9][0-9]|10[0-4])$/" 
 data-val-regex="The field must be in range 0 to 104."
>

What am I missing to make this properly validate? Did I miss a call to the jquery validator when rendering this dynamically or is the problem elsewhere?

1 Answer 1

1

No clue why the regex doesn't validate properly and am still open to explanations! The regex is formed right, and I even tried a couple different scenarios all which did not properly validate. I was at a loss until I discovered a masterfully written blog post: http://xhalent.wordpress.com/2011/01/21/arbitrary-client-side-validation-in-asp-net-mvc-3/

As a result, I made the following changes:

data-val-regex-pattern="/^(?:[0-9]|[1-9][0-9]|10[0-4])$/" 
data-val-regex="The field must be in range 0 to 104."

Became

data-val-range="The field must be in range 0 to 104." 
data-val-range-min="0" 
data-val-range-max="104"

Works like a charm.

Sign up to request clarification or add additional context in comments.

2 Comments

I know this is a bit old, but I noticed that writing the pattern starting and finishing with a '/', causes some unexpected behaviors. Have you tried without the slashes?
@Dorival - I am not sure if I tried without the slashes, but I assume so? It was a while ago. I was never able to make the regex properly work in all of my testing scenarios. I moved to the data value ranges and custom validation for more complex situations. If you are able to get the regex to work feel free to please post an answer with your findings and I will review if it is more relevant than this workaround.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.