Helen Araya
Helen Araya

Reputation: 1946

allow only certain chars in an input control

I am using jquery unobtrusive validation and my input looks like. so my input is allowed to have

<input class="FormElement form-control input-validation-error" data-val="true" data-val-required="The ID field is required." 
id="ID" name="ID" value="6" data-val-regex-pattern="^\d{1,12}(\.\d{0,3})?$|^\.\d{1,3}$" data-val-regex="You are allowed up to 12 digits in front of the decimal and 3 behind." aria-required="true" aria-invalid="true" aria-describedby="ID-error">

valid samples:

12
12.44

Invalid samples

12.3333333333333
12.
123456789045555.9
asdsadsadas
12.sw

The question is how do I disallow the user from even trying to enter values other than numbers and a period(.) before the validation catches it.

Upvotes: 0

Views: 44

Answers (1)

MLAlex
MLAlex

Reputation: 38

There is a type attribute for the input element that you need to set to "number" like so:

<input type="number" class="FormElement form-control input-validation-error" data-val="true" data-val-required="The ID field is required." 
id="ID" name="ID" value="6" data-val-regex-pattern="^\d{1,12}(\.\d{0,3})?$|^\.\d{1,3}$" data-val-regex="You are allowed up to 12 digits in front of the decimal and 3 behind." aria-required="true" aria-invalid="true" aria-describedby="ID-error">

Upvotes: 2

Related Questions