Reputation: 99
I am using the jQuery Validation plugin and on a field only want the user to have the opportunity to input only four numbers {1 or 2 or 3 or 4} respectively.
$.validator.addMethod(
"onetofourOnly",
function(value, element) {
// need to update here
return value.match(/(?:[0-4]){0,1}/);
},
"Please enter a number from 1 to 4."
);
Here I call the rule:
$("#myform").validate({
rules: {
numberField: {
onetofourOnly : true
},
}
});
My logic is wrong though, so what would be the approriate regex for specifying those four numbers I want and nothing else?
Upvotes: 1
Views: 1103
Reputation: 11051
Just use this regex:
/^[1-4]$/
Here is a regex demo.
>>> var re = /^[1-4]$/
>>> re.test("1")
... true
>>> re.test("0")
... false
Upvotes: 1