Click here to Skip to main content
13,140,231 members (49,293 online)
Rate this:
Please Sign up or sign in to vote.
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="">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
//* Validate Date Field script- By
//* For this script and 100s more, visit
//* 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.")
if (returnval==false)
return returnval
    <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">
<asp:TextBox runat="server" ID="txtid" Text='<%#Bind("Eid")%>'></asp:TextBox>
<asp:TextBox runat="server" ID="txt123" Text='<%#Bind("Date")%>'></asp:TextBox>
<asp:Button  runat="server" ID="btn123" OnClientClick="return checkdate(this.mydate);" Text="Click Me"  CommandName="Update" />
<asp:Label runat="server" ID="lblMessage"></asp:Label>

Code Behind Code:
 protected void gd_RowUpdating(object sender, GridViewUpdateEventArgs e)
        if (cnn.State == ConnectionState.Closed)
        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);
        lblMessage.Text = "Updated";
Posted 19-Jun-12 2:18am
Updated 19-Jun-12 3:25am
Sandeep Mewara 20-Jun-12 6:58am
What problem?

1 solution

Rate this: bad
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")%>');" 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2017
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