Reputation: 3
i'm trying to validate a field to include everything except letters but the following only works on the first character i enter. So if i enter '123a' the test method returns true.
$.tools.validator.fn("input#Phone", "Please enter a valid phone number.", function(input, value) {
var pass;
var rgx = /[^a-z]/gi;
if ( rgx.test(value)
|| (value == "")
|| (value == $(input).attr("placeholder"))) {
$(input).removeClass("invalid");
pass = true;
} else {
$(input).addClass("invalid");
pass = false;
}
return pass;
}
Upvotes: 0
Views: 356
Reputation: 1467
for only numeric:
RegExp(/^[^a-zA-Z]$/i)
for phone number you can use
RegExp(/^[0-9 -()+]{6,20}$/i)
Upvotes: 0
Reputation: 191729
You're only matching against a single character.
/^[^a-z]$/i
This ensures that the entire string is non-letters.
Upvotes: 1