Tuesday, June 4, 2013

How to validate email in asp.net using Javascript

Introduction: In this post I try to explain how we can validate the Email textbox for Valid Email Address and insert value into Database.
validate email in asp.net using Javascript

Description:
I have created a table EMPLOYEE_DETAIL. Here EMPLOYEE_ID is primary key.
EMPLOYEE_ID
int
EMPLOYEE_EMAIL
varchar(50)

After that add a new webform to project. Drag and drop the Textbox and Button from Toolbox as mention below:
<table border="1px solid">
    <tr><td><b>Validate the Valid Email Addres using Javascript</b></td></tr>
    <tr><td>
    <table><tr><td>
    Employee Email Address:</td><td><asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
    </td></tr>
    <tr><td>&nbsp;</td><td><asp:Button ID="btnsave" runat="server" Text="Save"
            OnClientClick="return ValidateEmail();" onclick="btnsave_Click"/></td></tr>
    </table>
    </td></tr>
    </table>


Now add the below mention Javascript between the Head tag (Before close of Head tag):
<script language="javascript" type="text/javascript">
      function ValidateEmail() {
          var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
          var emailAddress = document.getElementById("<%= txtemail.ClientID %>").value;
          var valid = emailRegex.test(emailAddress);
          if (!valid) {
              alert("Please Enter Valid Email address");
              return false;
          } else
              return true;
      }
  </script>

Go to .aspx.cs page and write below mention code:

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        btnsave.Attributes.Add("onclick", "return ValidateEmail()");
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("Insert into EMPLOYEE_DETAIL(EMPLOYEE_EMAIL) values (@EMPLOYEE_EMAIL)", con);
        con.Open();
        cmd.Parameters.AddWithValue("@EMPLOYEE_EMAIL", txtemail.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        cmd.Dispose();
        txtemail.Text = "";
    }

In VB (.aspx.vb)

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ToString())

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        btnsave.Attributes.Add("onclick", "return ValidateEmail()")
    End Sub
    Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim cmd As New SqlCommand("Insert into EMPLOYEE_DETAIL(EMPLOYEE_EMAIL) values (@EMPLOYEE_EMAIL)", con)
        con.Open()
        cmd.Parameters.AddWithValue("@EMPLOYEE_EMAIL", txtemail.Text)
        cmd.ExecuteNonQuery()
        con.Close()
        cmd.Dispose()
        txtemail.Text = ""
    End Sub
  

Now run the project and check the result.

Related Articles on Validation:
Ø  How to Validate Fileupload Control in Asp.netusing Javascript

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment