So, I suck terribly at regex and javascript. As such, when I realized I needed to validate a form field via jquery to ensure that at least one alpha character was used, I decided to ask you fine folks for some help.
I want to allow user to use apostrophes, spaces, dashes, periods, underscores, and alphanumeric chars in this field. They must, however, use at least one alpha character.
Here is the validator method I created, which does everything but ensure that at least one alpha char is used.
$.validator.addMethod("nameRegex", function(value, element) {
return this.optional(element) || /[^a-z/g]+/gi.test(value) || !/[^\.\'\:\-_ a-z0-9]+/gi.test(value);
}, "Name must contain at least one letter.");
Any tips/hints/insights/insults? Thanks everyone!
I have had luck doing a look ahead for the required letter then matching only on valid characters for the minimum length. In my example I use a minimum length of 8 characters with valid characters of apostrophe, space, hyphen, period, underscore, and alphanumeric characters. The ‘\w’ character class is suppose to match underscore and alphanumerics I believe. It seems to work in my tests:
EDIT: I added a ‘?’ after the ‘*’ in the look ahead to make the quantifier non-greedy. Also I should note that I tested this in Chrome and obviously my test cases are not conclusive. I hope this helps though. Another approach is to match zero to many valid characters, at least 1 letter, then zero to many valid characters. Something like: