EverTheLearner
EverTheLearner

Reputation: 7210

jQuery/Javascript custom email validation not working

I didn't want to us a plugin for simple email validations so i tried to create my own but it doesn't work. Its always returning false. Here is my code:

var regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i;

if(regex.test($("#email").val()))
{
    //pass
}

What am I doing wrong? Thank you!

Upvotes: 0

Views: 528

Answers (5)

Rafay
Rafay

Reputation: 31043

may be this will solve the problem

var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(rege.test($('#email').val()))
{ 
  //do something
}

Upvotes: 0

sharmila
sharmila

Reputation: 1533

Try this code..

 <script language="javascript" type="text/javascript">
 function EmailValidation(email) {

     var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
     if (!filter.test(email.value)) {
         alert('Please provide a valid email address');
         email.focus;
         return false;
     }
 }

and the HTML..

 <div>
    <asp:TextBox ID="TextBox1" runat="server" onchange="EmailValidation(this)"></asp:TextBox>
</div>

Upvotes: 0

deepi
deepi

Reputation: 1081

try this regular expression :

var regex= /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

Upvotes: 0

epascarello
epascarello

Reputation: 207557

I would assume that \A is supposed to be ^ and \z is supposed to be $.

Upvotes: 3

Null Pointer
Null Pointer

Reputation: 9309

Here is the regx i am using in my project.It works fine

                   var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                    if (emailReg.test($('#Email').val())) {    
                        //pass 
                    }  

Upvotes: 0

Related Questions