GridView中的Javascript日期验证不起作用 [英] Javascript Date Validation in GridView Not Working

查看:51
本文介绍了GridView中的Javascript日期验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

由于我是ASP .net的新手,因此在验证GridView中文本框上的日期时遇到问题.我想在数据库中更新之前验证日期.请提出任何建议.

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();
   }

推荐答案

///基本检查格式有效性 var returnval = false 如果(!validformat.test(input.value)) alert(无效的日期格式.请更正并再次提交.") else {//详细检查有效日期范围 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) 如果((dayobj.getMonth()+ 1!= monthfield)||((dayobj.getDate()!= dayfield)|||(dayobj.getFullYear()!= yearfield)) alert(检测到无效的日,月或年范围.请更正并再次提交.") 别的 returnval = true } 如果(returnval == false)input.select() 返回returnval } < /script > < /head > < 正文 > < 表单 提交 =" 返回checkdate(this.mydate)" 短跑 =" >> < asp:GridView 运行 =" ID gd " AutoGenerateColumns false" 继续更新 gd_RowUpdating" onrowcommand =" 进行编辑 gd_RowEditing" < > < asp:TemplateField > < ItemTemplate > < asp:TextBox 运行 =" ID txtid " 文本 <% #Bind(" )%> ' > < /asp:TextBox > < /ItemTemplate > < /asp:TemplateField > < asp:TemplateField > < ItemTemplate > < asp:TextBox 运行 =" ID txt123 " 文本 <% #Bind(" )%> ' > < /asp:TextBox > < /ItemTemplate > < /asp:TemplateField > < asp:TemplateField > < ItemTemplate > < asp:Button runat =" ID btn123" OnClientClick =" 文本 =" 点击我" CommandName 更新" / < /ItemTemplate > < /asp:TemplateField > < /列 > < /asp:GridView > < asp:Label 运行 =" ID lblMessage " < > < /form > < /body > < /html >
/ //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();
   }


尝试绑定事件,如下所示
try to bind the event as given below
OnClientClick="return checkdate(''<%#DataBinder.Eval(Container.DataItem,"Date")%>'');" 


这篇关于GridView中的Javascript日期验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆