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!

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

fromcomment: "want to perform a check that there are no numeric characters"
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.