Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday 4 March 2015

We can validate Email-ID using ASP.NET regular expression or by using jquery, so that user can enter valid email id. Some by mistake user enter the invalid emalid (Wrong format). We can solve this by providing him proper guideline by using regular expression validation with the help of jquery, which will help user to enter valid Email id.

This article demonstrates  the jQuery validation to validate email id. It will prompt you to enter the email address if it is left blank on submitting.And if invalid email address is entered then validation will be failed and show error message "Invalid Email Address. Please enter valid email e.g abc@domain.com". If user enter valid email address then further processing will be done and print the message "Valid Email address" in the label as in this example.
In the <Head > Section of the design page: create the jQuery function to validate the email address.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript">
    $(document).ready(function () {
        $("#btnValidate").click(function () {
            var EmailId = $("#txtEmailId").val();
            if ($.trim(EmailId).length == 0) {
                alert("Please Enter Email Address");
                return false;
            }
            if (validateEmailAddress(EmailId)) {             
                return true;
            }
            else {
                alert('Invalid Email Address.Please enter valid email e.g abc@domain.com');
                return false;
            }
        });
    });
    function validateEmailAddress(EmailId) {
        var expr = /^[a-zA-Z0-9._]+[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;
        if (expr.test(EmailId)) {
            return true;
        }
        else {
            return false;
        }
    }
</script>
In the <Body> tag place a TextBox control , Button control and Label control as:
<fieldset style="width:300px;">
<legend>Validate Email address Using jQuery in asp.net</legend>
<table>
<tr>
<td>Email Address:</td>
<td><asp:TextBox ID="txtEmailId" runat="server" Width="177px"/></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnValidate" runat="server" Text="Validate"
        onclick="btnValidate_Click" />
    <asp:Label ID="lblStatus" runat="server" Text="" style="color: #009933"></asp:Label>
    </td>
</tr>
</table>
</fieldset>
C#.Net Code  
Write the following code on Validate button click event

protected void btnValidate_Click(object sender, EventArgs e)
    {
        lblStatus.Text = "Valid Email Address";
    }

VB.Net Code 
First Design the form as shown in C#.net section but just change the
line <asp:Button ID="btnValidate"runat="server" Text="Validate" onclick="btnValidate_Click" />
 with the line <asp:Button ID="btnValidate" runat="server" Text="Validate"/>
Write the following code on Validate button click event

Protected Sub btnValidate_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlesbtnValidate.Click
        lblStatus.Text = "Valid Email Address"

    End Sub

0 comments:

Post a Comment