$.validator.addMethod(\'AZ09_\', function (value) {
return /^[a-zA-Z0-9.-_]+$/.test(value);
}, \'Only letters, numbers, and _-. are allowed\');
Generally with hyphen (-
) character in regex, its important to note the difference between escaping (\-
) and not escaping (-
) the hyphen because hyphen apart from being a character themselves are parsed to specify range in regex.
In the first case
, with escaped hyphen (\-
), regex will only match the hyphen as in example /^[+\-.]+$/
In the second case
, not escaping for example /^[+-.]+$/
here since the hyphen is between plus and dot so it will match all characters with ASCII values between 43 (for plus) and 46 (for dot), so will include comma (ASCII value of 44) as a side-effect.