Tuesday, May 11, 2010

Client Side Validation Web Form Using Java Script

The following Shows the Client Side Validation Web Form Using Java Script


Source Code:

Java Script:


function Submit1_onclick() {

    var t1,t2,t3,t4,t5;
    t1 = document.getElementById("TextBox1");  
    if(t1.value == "")
    {
        alert("enter the Name ");
        document.getElementById("TextBox1").focus();
        //t1.value=" " ;              
        return false;
    }
  
    t2 = document.getElementById("TextBox2");  
    if(t2.value == "")
    {
        alert("enter the Reg Number");
        document.getElementById("TextBox2").focus();
        return false;
      
    }
  
    t3 = document.getElementById("TextBox3");  
    if(t3.value == "")
    {
        alert("enter the Pass Word");
        document.getElementById("TextBox3").focus();
        return false;
      
    }
  
    t4 = document.getElementById("TextBox4");  
    if(t4.value == ""  )
    {
        alert("enter the Confirm Pass Word");
        document.getElementById("TextBox4").focus();
        return false;
      
    }
  
    if( t3.value != t4.value)
    {
    alert (" Pass Word Mismatch ");
    return false;
    }
  
     t5 = document.getElementById("TextBox5");  
        if(t5.value == "")
        {
        alert("enter the e-mail");
        return false;
        }
     var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
    
     var emailid = document.getElementById("TextBox5").value;
    
     var matchArray = emailid.match(emailPat);
    
     if (matchArray == null)
        {
               alert("Your email address seems incorrect. Please try again.");
              
               document.getElementById("TextBox5").focus();
              
               return false;
        }
  
  
    t6 = document.getElementById("TextBox6");  
    if(t6.value == "")
    {
        alert("enter the Date of Birth");
        return false;
      
    }
    //var dateform =/(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$/; //mm/dd/yyyy
   alert("Please click  the Submit Button");    
    return true;
}



The Default.aspx.cs file is

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // if(!(Page.IsPostBack))
        // only executes for the first time of the page load event.. initially alone
        //Button1.Attributes.Add("onclick", "Submit1_onclick();");    

    }

    public string name1, password1, email1, dob1;
    public int regno1;
    SqlCommand cmd;
  
    //submit
    protected void Button1_Click(object sender, EventArgs e)
    {

            string constr, query;
            constr = "Data Source=6;Initial Catalog=employee123;User ID=sa;Password=123";
            SqlConnection conn = new SqlConnection(constr);
            try
            {
              
                /*query = "Insert into regform values(@name,@regno,@password,@email,@DOB)";
                cmd = new SqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@name",TextBox1.Text);
                cmd.Parameters.AddWithValue("@regno",TextBox2.Text);
                cmd.Parameters.AddWithValue("@password",TextBox3.Text);
                cmd.Parameters.AddWithValue("@email", TextBox5.Text);
                cmd.Parameters.AddWithValue("@DOB", TextBox6.Text);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                Response.Write("the value updated successfully");*/

                cmd = new SqlCommand("regforms_sp", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text;
                cmd.Parameters.Add("@regno", SqlDbType.VarChar).Value = TextBox2.Text;
                cmd.Parameters.Add("@passwords", SqlDbType.VarChar).Value = TextBox3.Text;
                cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = TextBox5.Text;
                cmd.Parameters.Add("@DOB", SqlDbType.VarChar).Value = TextBox6.Text;
                conn.Open();
                int rows = cmd.ExecuteNonQuery();
                conn.Close();    
              
          
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }
        //display
    protected void Button2_Click(object sender, EventArgs e)
    {
        string constr;
      
        constr = "Data Source=6;Initial Catalog=employee123;User ID=sa;Password=123";
      
        SqlConnection conn = new SqlConnection(constr);
        try
        {    
                
          string sql123 = "select * from regforms ";
          SqlDataAdapter  da = new SqlDataAdapter(sql123,conn);      
          DataSet ds = new DataSet();                
          da.Fill(ds);
          int y = 0;
            for (int x=1; x < ds.Tables[0].Rows.Count;y++)
            {
                Response.Write(ds.Tables[0].Rows[x].ItemArray[y].ToString());
                if (y == 4)
                    break;
            }
      
        }
        catch (Exception ex)
        {
            Response.Write(ex);

        }

    }
    protected void TextBox6_TextChanged(object sender, EventArgs e)
    {

    }
}

No comments:

Post a Comment