Click here to Skip to main content
15,913,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i had ask this question previously also but could not frame it properlly. Now i am asking it again so please forgive me and help.
As subject suggest is it possible to validate fields through javascript and then enter validated data in database .
here i want to use c# coding to enter data in database. Is it possible to do all this through single click of a (single) button.

small demo ? links ?

thanks
anoop
Posted
Updated 7-Nov-11 19:18pm
v2

Yes on single button click you can call Javscript and codebehind also. it is very basic technique.

button has two click events.
1. server click
2. client click.

JavaScript
//check textbox empty
function CallME()
{
if (document.getElementById("txtBox").value == "")
   {
      alert('please enter value');
      return false;
   }
}


call above javascript on onclientclick event onserverclick call code behind method.

first your javascript get's executed and if the textbox is not empty then only it will call code behind otherwise it prompt message and stop.

hope it helps.
 
Share this answer
 
On your button tag, use OnClientClick and call a javascript validation function from that. If it's not valid, then return false from script. Page won't get postback. If it is valid, page will be posted back.

Kind of this
http://vijaymodi.wordpress.com/2007/06/08/button-onclick-and-onclientclick-2/[^]
 
Share this answer
 
v2
You can write all the validation code using javascript function like

C#
<script language="javascript" type="text/javascript">
function Validation()
    {
      var txtfirstname1=document.getElementById('<%=txtfirstname.ClientID%>');
      var txtlastname1=document.getElementById('<%=txtlastname.ClientID%>');
      var txtemail1=document.getElementById('<%=txtemail.ClientID%>');
       if(txtfirstname1.value=="")
      {
      alert("Please Enter Firstname");
      txtfirstname1.focus();
      return false;
      }
      if(txtlastname1.value=="")
      {
      alert("Please Enter Lastname");
      txtlastname1.focus();
      return false;
      }
      if(txtemail1.value=="")
      { 
       alert("Please Enter Email");
       txtemail1.focus();
       return false;
      }
      var s=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
      if(!s.test(txtemail1.value))
      {
      alert("Enter valid Email");
      txtemail1.focus();
      return false;
      }
}


You can call this function like

VB
<asp:Button ID="Btnsubmit" runat="server" Text="Submit"                                      OnClick="Btnsubmit_Click" OnClientClick="javascript:return Validation()" />


In Submit button click event, write below code

C#
protected void Btnsubmit_Click(object sender, ImageClickEventArgs e)
   {

string s = "INSERT into UserDetails (email,firstname,lastname) values('" + txtemail.Text + "','" + txtfirstname.Text + "','" + txtlastname.Text + "')";
                SqlCommand cmd;
                cn = new SqlConnection(conection.getconnection());
                cmd = new SqlCommand(s, cn);
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
}


Here "cn" means your connectionstring variable
 
Share this answer
 
v2
simple way you can do this

your button should be like this

XML
<asp:Button ID="btnSubmit" runat="server"                                                             OnClientClick="javascript:return Validate();" OnClick="btnSubmit_Click" />




write this script in head part of your page

<script language="text/javascript">
function Validate()
   {
//txtArea is the textbox id
   if(document.getElementById('<%=txtArea.ClientID%>').value=="")
    {
        alert('Please Enter Area Name');
        return false;
    }
    else
        return true;  
   }   
   
</script>


add this in your code behind file

C#
protected void btnSubmit_Click(object sender, ClickEventArgs e)
{
//your code behind code
//inserting the data to database

}


if textbox is empty then code behind code will not be executed
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900