Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
Hi Friends,
 
As I am New to ASP .net , I am facing problem while validating a date on a textbox in GridView. I want to validate the date before updating in database. Please give any suggestions.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
 
/**--------------------------
//* Validate Date Field script- By JavaScriptKit.com
//* For this script and 100s more, visit http://www.javascriptkit.com
//* This notice must stay intact for usage
---------------------------**/
 
function checkdate(input){
var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
var returnval=false
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correct and submit again.")
else{ //Detailed check for valid date ranges
var dayfield=input.value.split("/")[0]
var monthfield=input.value.split("/")[]
var yearfield=input.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
else
returnval=true
}
if (returnval==false) input.select()
return returnval
}
 
</script>
</head>
<body>
    <form  onsubmit ="return checkdate(this.mydate) " runat="server">
 
<asp:GridView runat="server" ID="gd" AutoGenerateColumns="false" onrowupdating="gd_RowUpdating" onrowcommand="gd_RowCommand" onrowediting="gd_RowEditing">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ID="txtid" Text='<%#Bind("Eid")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ID="txt123" Text='<%#Bind("Date")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button  runat="server" ID="btn123" OnClientClick="return checkdate(this.mydate);" Text="Click Me"  CommandName="Update" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label runat="server" ID="lblMessage"></asp:Label>
</form>
</body>
</html>
 
Code Behind Code:
 protected void gd_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (cnn.State == ConnectionState.Closed)
            cnn.Open();
        GridViewRow grow = gd.Rows[e.RowIndex];
        TextBox txt123 = (TextBox)grow.FindControl("txt123");
        TextBox txtid = (TextBox)grow.FindControl("txtid");
 
        SqlCommand cmd = new SqlCommand("update emp_details set date='" + txt123.Text + "' where Eid="+Convert.ToInt32(txtid.Text )+ "", cnn);
        cmd.ExecuteNonQuery();
        GridBind();
        lblMessage.Text = "Updated";
        cnn.Close();
    }
Posted 19-Jun-12 2:18am
Rockstar_5.7K
Edited 19-Jun-12 3:25am
v2
Comments
Sandeep Mewara at 20-Jun-12 6:58am
   
What problem?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try to bind the event as given below
OnClientClick="return checkdate('<%#DataBinder.Eval(Container.DataItem,"Date")%>');" 
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 547
1 Member 11120668 188
2 ChauhanAjay 180
3 Maciej Los 165
4 CHill60 135
0 Sergey Alexandrovich Kryukov 9,735
1 OriginalGriff 6,987
2 CPallini 5,835
3 George Jonsson 4,017
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100