Hi. I am learning JavaScript language. I want to write JavaScript function for user enters valid Email-Id. If user enter email id in wrong format it will give error message. Your help would be greatly appreciated. Thanks in advanced.
Hi. I am learning JavaScript language. I want to write JavaScript function for user enters valid Email-Id. If user enter email id in wrong format it will give error message. Your help would be greatly appreciated. Thanks in advanced.
Last edited by nitesh14; 04-26-2010 at 03:52 AM.
Try out this code, when you run this function you can check the user enter valid email id or not:
Code:function checkEmailID(frmName,fldName) { if (!(isNull(eval("document."+frmName+"."+fldName+".value")))) { if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(eval("document."+frmName+"."+fldName+".value"))) { return true; } else { alert("Invalid E-mail ID! Please re-enter."); eval("document."+frmName+"."+fldName+".focus()"); return false; } } else { alert(" Please provide E-mail ID. "); eval("document."+frmName+"."+fldName+".focus()"); return false; } }
Refer following JavaScript for user enter valid email id:
Code: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) == false) { alert('Invalid Email Address'); return false; } }
If you are use asp.net then use following code. Hope this helps you out with your problem.
Code:<asp:textbox id="txt_email" runat="server"/> <asp:RegularExpressionValidator id="email_valid" runat="server" ControlToValidate="txt_email" ValidationExpression=".*@.*..*" ErrorMessage="* Enter Valid Email-Id." display="dynamic"> </asp:RegularExpressionValidator>
Bookmarks