Validate Email with RegularExpression validation using JavaScript

Last Reply 11 months ago By dharmendr

Posted 11 months ago

My requirement am using RegularExpressionValidator for email

textbox  this need to fire on button onclientclick function using javascript.


Posted 11 months ago Modified on 11 months ago

Hi amar,

Refer below sample.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        function ValidateEmail() {
            var regx = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
            var email = document.getElementById("txtEmail").value;
            if (!regx.test(email)) {
                alert("Invalid email address.");
            }
            else {
                alert("Email address is valid.");
            }
        }
    </script>
</head>
<body>
    <input name="txtEmail" type="text" id="txtEmail" />
    <input type="button" value="Validate" onclick="ValidateEmail();" />
</body>
</html>

Demo

You can change the html control with the below asp controls.

<asp:TextBox ID="txtEmail" runat="server" />
<asp:Button Text="Validate" runat="server" OnClientClick="return ValidateEmail();" />