3

I have been developing a script and I'm stumped right now. I have tried many different Regex from Google and non have worked, this is my code:

$(document).ready(function() {
    var email_check = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/;
    num = 0;
    $('#email').focus(function() {
        $('#email_check').show();
        $(this).keyup(function() {
            error = 0;
            var email = $('#email').val();
            num = num+1;
            if(!email_check.test(email)) {
                error = 1;
            }
            if(error == 0) {
                $('#email_check').html('O'+num+email+error);
            }else if(error == 1) {
                $('#email_check').html('X'+num+email+error);
            }
        });
    });
});

Any help is great, thanks!

2 Answers 2

12

Set the case insensitive flag (i):

var email_check = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i;
Sign up to request clarification or add additional context in comments.

2 Comments

Worked! Thanks, something so simple but so irritating haha!
does email address can be start with %. I think even % is not allowed
2

You could skip writing the regex and use the jQuery validation plugin for this task. There's an example here of validating an email address.

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.