Reputation: 153
I have this script that return true if in my string are only letters and spaces.
jQuery.validator.addMethod("lettersonly", function (value, element) {
return this.optional(element) || /^[a-z\s]+$/i.test(value);
}, "Solo lettere ammesse");
How can add numbers and "," "-" like allow values?
Upvotes: 0
Views: 377
Reputation: 57105
A-z
also covers characters with ASCII value 91 to 96 which is undesirable as per the requirement mentioned in the question. Use A-Za-z
instead. Thus, the correct regex would be:
^[A-Za-z0-9-,\s]*$
Use ^[A-z0-9-,\s]*$
jQuery.validator.addMethod("lettersonly", function (value, element) {
return this.optional(element) || /^[A-z0-9-,\s]*$/i.test(value);
}, "Solo lettere ammesse");
Upvotes: 3