Search code examples
jqueryregexstringalphanumeric

Allow number, letters and any symbol in my string


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?


Solution

  • 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/