时间输入的验证

前台js验证:

<textarea class="c-sharp" rows="15" cols="50" name="code">&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;validateCode.aspx.cs&quot; Inherits=&quot;practise.validateCode&quot; %&gt;&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;head runat=&quot;server&quot;&gt; &lt;title&gt;&lt;/title&gt; &lt;mce:script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&gt;&lt;!-- function validateDate() { var t_date = document.getElementById(&quot;t_date&quot;).value; if (t_date != &quot;&quot; || t_date != null) { var dateReg = /^(/d{4})(-|//)(/d{1,2})/2(/d{1,2}) (/d{2})(:)(/d{2})/6(/d{2})$/; /*注: /d表示的是正整树 只能输入n位的数字:&quot;^/d{n}$&quot;。 只能输入至少n位的数字:&quot;^/d{n,}$&quot;。 只能输入m~n位的数字:。&quot;^/d{m,n}$&quot; 2表示第二个括号的内容,/2表示内容和(-|//)一样的,前面年和月用的什么,后面月和日也得用什么 6表示第六个括号的内容,/6表示内容和(:)一样的,前面时间和分用的什么,后面分和秒也得用什么 */ if (dateReg.test(t_date)) { var date = t_date.match(dateReg); if (date[1] &lt; 1 || date[3] &lt; 1 || date[4] &lt; 1) { alert(&quot;日期需大于1!&quot;); return false; } switch (parseInt(date[3])) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (date[4] &gt; 31) { alert(&quot;此月份最多只有31天!&quot;); return false; } break; case 2: if ((date[1] % 4 == 0 &amp;&amp; date[4] &gt; 29) || (date[1] % 4 != 0 &amp;&amp; date[4] &gt; 28)) { alert(&quot;此年的二月份日期输入错误!&quot;); return false; } break; case 4: case 6: case 9: case 11: if (date[4] &gt; 30) { alert(&quot;此月份最多只有30天!&quot;); return false; } break; default: alert(&quot;月份输入错误!&quot;); return false; break; } if (date[5] &gt; 24 || date[7] &gt; 60 || date[8] &gt; 60) { alert(&quot;时间输入错误!&quot;); return false; } } else { alert(&quot;日期输入错误,格式:0000-00-00 00:00:00!&quot;); return false; } } alert(&quot;时间:&quot; + t_date); return true; } // --&gt;&lt;/mce:script&gt;&lt;/head&gt;&lt;body&gt; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;asp:ScriptManager ID=&quot;ScriptManager1&quot; runat=&quot;server&quot; /&gt; &lt;div&gt; &lt;input type=&quot;text&quot; id=&quot;t_date&quot; /&gt; &lt;input type=&quot;button&quot; id=&quot;b_validate&quot; value=&quot;javaScript验证&quot; runat=&quot;server&quot; onclick=&quot;validateDate();&quot; /&gt; &lt;hr style=&quot;width: 100%; height: 5px; background-color: Gray&quot; /&gt; 开始时间:&lt;asp:TextBox ID=&quot;tb_startDate&quot; runat=&quot;server&quot; /&gt;&lt;br /&gt; 结束时间:&lt;asp:TextBox ID=&quot;tb_endDate&quot; runat=&quot;server&quot; /&gt;&lt;br /&gt; &lt;asp:Button ID=&quot;btn_validate&quot; Text=&quot;后台服务器端验证&quot; runat=&quot;server&quot; OnClick=&quot;btn_validate_Click&quot; /&gt; &lt;/div&gt; &lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</textarea>

 

后台验证:

<textarea class="c-sharp" rows="15" cols="50" name="code">using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace practise{ public partial class validateCode : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ScriptManager1.RegisterAsyncPostBackControl(btn_validate); //ajax无刷新技术,在这里注册按钮这样就不用写updatePanel了 } /// &lt;summary&gt; /// 验证时间是否输入正确 /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; private bool validateDate(ref string message) { bool result = false; try { Convert.ToDateTime(tb_startDate.Text); try { Convert.ToDateTime(tb_endDate.Text); if (!string.IsNullOrEmpty(tb_startDate.Text) &amp;&amp; !string.IsNullOrEmpty(tb_endDate.Text)) { DateTime maintainStartDateTime = Convert.ToDateTime(tb_startDate.Text); DateTime maintainEndDateTime = Convert.ToDateTime(tb_endDate.Text); if (maintainStartDateTime &gt; maintainEndDateTime) { message = &quot;开始日期不能大于结束日期!&quot;; result = false; } else { result = true; } } } catch (Exception) { message = &quot;结束时间输入错误!&quot;; tb_endDate.Focus(); result = false; } } catch (Exception) { message = &quot;开始时间输入错误!&quot;; tb_startDate.Focus(); result = false; //throw;注:throw上面有处理就不用了 } return result; } protected void btn_validate_Click(object sender, EventArgs e) { string message = string.Empty; if (validateDate(ref message)) { ScriptManager.RegisterStartupScript(this, GetType(), &quot;&quot;, &quot;alert('ok!');&quot;, true); } else { ScriptManager.RegisterStartupScript(this, GetType(), &quot;&quot;, &quot;alert('&quot; + message + &quot;');&quot;, true); } } }}</textarea>

 

你可能感兴趣的:(JavaScript,Date,exception,server,asp,textbox)