0

I want to apply javascript email validation in my project so please some suggest me the exact code for that because i just want to apply it only for regular expression and i don't want to add any plug in for it. is it possible to write some code in source file and apply to email field.

Thanks in advance

Ram Singh
  • 6,664
  • 35
  • 100
  • 166
  • possible duplicate of [Validate email address in Javascript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Chris J Apr 03 '12 at 16:52

3 Answers3

3
function validate(form_id,email) {

   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(!reg.test(address)) {     
      alert('Invalid Email Address');
      return false;
   }
}
Sarath
  • 9,030
  • 11
  • 51
  • 84
1

you can add a regular expression validator to validate you email.

<asp:TextBox ID="txtEmailAddress" runat="server" ></asp:TextBox>
                        <asp:RegularExpressionValidator ID="valRegExEmail" runat="server" ControlToValidate="txtEmailAddress"
                            Display="None" ErrorMessage="Please give a valid email address" ValidationGroup="StaffAddValidation" ValidationExpression="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z\.][a-zA-Z]{1,3}$"></asp:RegularExpressionValidator> 

or if you want to check email from from javascript . check the SO post

Community
  • 1
  • 1
Mahesh KP
  • 6,248
  • 13
  • 50
  • 71
  • thanks mahesh i checked that link and i got what i really required. Thank You once again – Ram Singh Aug 05 '11 at 05:48
  • This will fail to validate domains that end .info and any others that are more than 3 chars long. It also doesn't allow for all the valid chars that can appear in an address. – Chris J Apr 03 '12 at 16:52
  • @ChrisJ: i have just given a sample regular expression, we can customize the regular expression according to our needs. To work with domains of length more than 4 you can change this area {1,3} according to our need. – Mahesh KP Apr 04 '12 at 03:07
  • I'm pointing out the flaws with using a reg-exp. It's much better to just accept accept something of the form [string]@[string], and not even attempt to validate either of the strings. You can get into a world of pain very quickly. There's a good article at http://isemail.info/about which outlines the pitfalls. – Chris J Apr 04 '12 at 07:48
0

You can use regular expression (/^([\w.-]+)@([\w-]+)((.(\w){2,3})+)$/i) to validate email address. var emailRegex = new RegExp(/^([\w.-]+)@([\w-]+)((.(\w){2,3})+)$/i);

var valid = emailRegex.test(emailAddress);
if (!valid) {
alert("Invalid e-mail address");
return false;
} 
else
 return true;

Get example code here: http://www.codegateway.com/2012/03/regex-for-email-validation-javascript.html Also see example how to validate email address using RegularExpressionValidator http://www.codegateway.com/2012/03/validate-email-format.html

Avinash
  • 199
  • 1
  • 2