How to Validate Textbox Using Javascript in - ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

How to Validate Textbox Using Javascript in

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

I have created a table EMPLOYEE_DETAIL. Here EMPLOYEE_ID is primary key.

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>
    Employee Name:</td><td><asp:TextBox ID="txtemployeename" runat="server"></asp:TextBox>
        <asp:Button ID="btnsave" runat="server" Text="Save"
            OnClientClick="return validation();" onclick="btnsave_Click" /></td></tr>

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)");
               return false;

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);
        cmd.Parameters.AddWithValue("@EMPLOYEE_NAME", txtemployeename.Text);
        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)
        cmd.Parameters.AddWithValue("@EMPLOYEE_NAME", txtemployeename.Text)
        txtemployeename.Text = ""
    End Sub

Now Run the application and check the result.

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

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.