0

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!

drudge
  • 35,471
  • 7
  • 34
  • 45
TakN
  • 87
  • 7

3 Answers3

2

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.

jesse reiss
  • 4,509
  • 1
  • 20
  • 19
1

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.

entonio
  • 2,143
  • 1
  • 17
  • 27
0

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}/)

Billy Moon
  • 57,113
  • 24
  • 136
  • 237