0

I want to allow _ inside the email.

but the regexp i have just filter this.

html,

<input id="external_email_input" maxlength="85" type="text">
    <button id='email'>Send</button>

js,

 $("#email").click(function(){
        var email = $("#external_email_input").val();
        if(!validateEmail(email)){
            alert("wrong format!!");
        }else{
            alert("correct!!");
        }
    });    




function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

jsfiddle,

http://jsfiddle.net/rRnwb/1/

if i put test@test_test.com

it returns false

how can i allow _ as a condition in here?

1
  • Just put it in the required character class. And validating email by regex is not always a good idea. Commented Jan 27, 2014 at 8:14

3 Answers 3

3

Use <input type="email" />. Remove all JavaScript. Problem solved.

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

1 Comment

<input type="email" /> works in IE8. It just won't have validation. But that's okay because validation should be server-side.
0

Modifying validation to include underscore (_) as suggested by Jerry in comments solves the problem.

Validation function:

function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"_]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9_]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

Updated fiddle: http://jsfiddle.net/rRnwb/2/

Comments

0

Your code was :

enter image description here

There is no _ to satisfy your input mail id after @ symbol.

So , you can use :

(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|((\w+\.)+[a-zA-Z]{2,}))

FIDDLE DEMO

Explanation : enter image description here

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.