Reputation: 87
I am trying to perform a few check but REGEX I just don't get the hang of. I am trying to check firstly for 3 characters and secondly want to perform a check that there are no numeric characters. The code is below and finally could some one answer is the regex syntax same in every language?
$(document).ready(function() {
var name=$('#firstname');
function validateName() {
//function nameCheck() {
if (name.val().length <=2) {
name.addClass('error');
$(".hidden_f").html("First Name must be 3 letters!");
} else if ($("#firstname.").val().match('[a-z]')) {
$(".hidden_f").html("No numbers!");
}
}
name.keyup(validateName);
});
And also if possible could some one give me a few examples so I can try and learn regex cheers!
Upvotes: 0
Views: 411
Reputation: 2173
You're passing a string, not a regexp, as the argument of match, so it's trying to Mach the sequence in the string literally as it is. Regexps are delimited by / / or created with new Regexp(escaped string).
As an aside, if you don't need the matches, you should use regexp.test(input) instead.
Upvotes: 1
Reputation: 4579
There are a few slightly different flavors of RegExp, but for the most part, RegExp is the same in every language with minor differences.
I think this is the best resource for RegExp : http://www.regular-expressions.info/
it's packed with articles and examples, truly an amazing website.
To match a phrase with 3 or more characters and no numeric characters you could use
var re = /[^0-9]{3,}/i
That should work.
Upvotes: 2
Reputation: 58521
Firstly you seem to have an error in the selector $("#firstname.")
should read $("#firstname")
As for regex - for 3 letters (no numbers) you can use match(/[a-zA-Z]{3}/)
Upvotes: 0