Validation in Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krisssgopi
    New Member
    • Apr 2010
    • 39

    Validation in Javascript

    Hi All,
    I am using java script for validating the textbox in my asp.net page. Validation is working fine but when i click the submit button the empty values are inserting in my database. Please help me how shall i overcome this problem.
    Code:
    function check(id,l)
    {
    var st;
    st=document.getElementById(id).value;
    if(st=="")
    document.getElementById(l).innerHTML="Enter User Name..";
    
    }
    this is for validation
    Code:
    Button1.Attributes.Add("onclick", "validate('" + TextBox1.ClientID + "','" + TextBox2.ClientID + "','" + DropDownList2.ClientID + "','" + Label4.ClientID + "')")
    Code:
     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim obj As New creatinguser
            Label5.Text = obj.create(TextBox1, TextBox2, DropDownList2)
        End Sub
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    #2
    You can use a required field validator control to solve this problem

    Comment

    • benwizzle
      New Member
      • May 2010
      • 72

      #3
      How about using the System.Data.Sql Client assembly to write the values to your database? I usually do parametrized query's like this:
      Code:
      SqlConnection con = new SqlConnection("connectionstring");
      SqlCommand cmd = new SqlCommand("sqlcommand", con)
      cmd.Connection.Open();
      cmd.ExecuteNonQuery();
      cmd.Connection.Close();
      Where "sqlcommand " is at is where you type your sql command such as an insert. For example if you were inserting a name based on a textbox.

      Code:
      SqlCommand cmd = new SqlCommand("INSERT INTO Table (Name) VALUES (@name)", con);
      cmd.Parameters.AddWithValue("@name", Textbox.Value);
      As far as the validation goes, I dont like using the validator controls nor javascript(sinc e it can be disabled) so I hard code everything. I usually make a method or function that returns a boolean. I then run that method/function on the button submit but only do the insert if the validation passes.

      Hope this helps. Let me know if you have any issues.

      Comment

      Working...