2

What regular expression should I use if I want the input to be equivalent to a specific value? For example, the input field must be "100". What would be the expression for this?

I've already tried:

<input type="text" pattern="[1-1][0-0][0-0]" maxlength="3" disabled required/>

The above attribute would work without disabled but doesn't work if the input is disabled.

What I'm trying to do is to offer the user the ability to insert total marks allotted to 2 exams (mids and finals). And the total's input is done by jquery and should be hundred. I'm looking for a simple solution using HTML5 that if the total's input is not equal to hundred, it would give a validation error.

Thanks.

2 Answers 2

4
<input type="number" name="foo" pattern="100" required>

Should do it.

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

6 Comments

Works fine for me: jsfiddle.net/74anbax0 (can't submit with for example "12", "1000", "10", "20" - only "100")
I think I found my culprit. My input field is disabled (value is being entered through js). Any way to keep the validation working with the disabled attribute? Example: jsfiddle.net/74anbax0/1
Hmm I'm not sure. But I don't get why you need to validate a field that no-one can enter a value into? Just make sure it doesn't contain the wrong pattern when you insert the value with JS?
it's not a good idea.. I think he needs to change it to a button with a specific value if that's the case.
Actually what I'm trying to do is to offer the user the ability to insert total marks allotted to 2 exams (mids and finals). And the total should be hundred... I was looking for a simple solution using HTML5 that if the total's input is not equal to hundred, it would give a validation error. Is this not possible using just html5?
|
0

you can use jquery to check the input's value. for that you don't need to use regular expression.

var inputvalue = $("input").val();
if(inputvalue == '100'){
 alert('value == 100');
}

2 Comments

I'm trying to use html5 validation, is it not possible to do it using that?
He specifically asked for a HTML5 pattern attribute solution. Besides, JS-validation of forms is SO 2012 :P

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.