문제

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?

도움이 되었습니까?

해결책

Update:

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]*$

Original answer:

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");

Good website for regex making and testing http://gskinner.com/RegExr/
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top