You must Sign In to post a response.
  • Category: JavaScript

    Valid Email Address via Javascript

    How to check valid email address in javascipt... I want if user enters wrong email address function should return message of wrong email address to the user...

    Plz Provide code as soon as possible

    Thanks in advance
  • #652555
    javascript
    function validateEmail(){
    var emailInput=document.getElementID('txt_emial').value;
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(elementValue);
    }

    html code
    <asp:button ID="Test" onClientClick="return validateEmail()" runat="server">

  • #652590
    You can also use the regular expression to validate the email address that will happen client side only using Regular expression validator.


    asp:RegularExpressionValidator ID="rev_email" runat="server" ErrorMessage="RegularExpressionValidator" controltovalidate="txt1"
    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></div>


    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #652601
    Please check the following code for Email Validation:


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Untitled Page</title>

    <script type="text/javascript">
    function CheckEmail(txt) {
    var Pattern = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    if (txt.value.search(Pattern) == -1) {
    alert('Invalid Email');
    txt.focus();
    return false;
    }
    return true;
    }

    <script>

    </head>
    <body>
    <form id="form1" runat="server">
    E-mail Address:
    <asp:TextBox ID="Email" runat="server" onblur="CheckEmail(this);"></asp:TextBox>
    </form>
    </body>
    </html>


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #652722
    Hi!
    >>You can use the following code for email validation

    function validateForm()
    {
    var x=document.forms["myForm"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
    {
    alert("Not a valid e-mail address");
    return false;
    }
    }

    >>Use the code when form is submitted

    Regards
    Kamlesh

    Regards
    Kamlesh Mohanty

  • #654153
    <script type="text/javascript">
    function CheckEmail(txt) {
    var format = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    if (txt.value.search(format) == -1) {
    alert( Email not correct');
    txt.focus();
    return false;
    }
    return true;
    }

    <script>


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.