开发者

JavaScript Email validation

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


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;
   }
}


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜