Thursday, June 6, 2013

How to Validate Textbox Using Javascript in Asp.net

Introduction: In this I try to explain how we can validate the textbox (require field) and insert its value into database.
Validate Textbox Using Javascript

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

Add a new webform to project. Drag and drop the controls from Toolbox.
<table border="1px solid">
    <tr><td><b>Validate the Required Field (Textbox) Using Javascript</b></td></tr>
    <tr><td>
    <table><tr><td>
    Employee Name:</td><td><asp:TextBox ID="txtemployeename" runat="server"></asp:TextBox>
    </td></tr>
    <tr><td>&nbsp;</td><td>
        <asp:Button ID="btnsave" runat="server" Text="Save"
            OnClientClick="return validation();" onclick="btnsave_Click" /></td></tr>
    </table>
    </td></tr>
    </table>

After that add the below mention Javascript in Head tag (before close of Head tag).
<script type="text/javascript">
       function validation() {
           if (document.getElementById("<%=txtemployeename.ClientID%>").value == "") {
               alert("Please Enter Employee Name (Required Filed)");
               document.getElementById("<%=txtemployeename.ClientID%>").focus();
               return false;
           }
       }
  </script>

Now go to .aspx.cs page and write the 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 validation()");
    }
protected void btnsave_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("Insert into EMPLOYEE_DETAIL(EMPLOYEE_NAME) values (@EMPLOYEE_NAME)", con);
        con.Open();
        cmd.Parameters.AddWithValue("@EMPLOYEE_NAME", txtemployeename.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        cmd.Dispose();
        txtemployeename.Text = "";
    }

In VB (.aspx.vb)

using System.Data;
using System.Data.SqlClient;
using 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_NAME) values (@EMPLOYEE_NAME)", con)
        con.Open()
        cmd.Parameters.AddWithValue("@EMPLOYEE_NAME", txtemployeename.Text)
        cmd.ExecuteNonQuery()
        con.Close()
        cmd.Dispose()
        txtemployeename.Text = ""
    End Sub

Now Run the application and check the result.

Related Articles on Validation:
Ø  How to validate email in asp.net using 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