如何使用比较验证器验证时间..? [英] How to validate time using compare validator..?

查看:107
本文介绍了如何使用比较验证器验证时间..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个带有掩码文本框的文本框,用于输入时间..



  <   asp:TextBox     ID   =  txtStartTime    runat   =  server    MaxLength   =  8    CssClass   =  WellInfoLables  

宽度 = 150 > < / asp:TextBox >
< asp:MaskedEditExtender ID = MaskedEditExtender_txtStartTime runat = server ClearMaskOnLostFocus = False Mask = 99:99 TargetControlID = txtStartTime < span class =code-attribute> MaskType = 时间 UserTimeFormat = TwentyFourHour

MessageValidatorTip = true / >

///////和////

< asp:TextBox ID = txtEndTime runat = server < span class =code-attribute> MaxLength
= 8 CssClass = WellInfoLables

<跨度 class =code-attribute>宽度 = 150 > < / asp:TextBox >
< asp:MaskedEditExtender ID = MaskedEditExtender_txtEndTime runat = server ClearMaskOnLostFocus < span class =code-keyword> = False
< span class =code-attribute>
掩码 = 99:99 TargetControlID = txtEndTime MaskType = 时间 UserTimeFormat = TwentyFourHour

MessageValidatorTip = true / >





现在另外我将一个comparevalidation控件添加到我的页面中......



 <   asp:CompareValidator     ID   =  ComValEndTime    runat   =  server    

ControlToValidate = txtEndTime ControlToCompare = txtStartTime

ErrorMessage = 结束时间大于开始时间

文字 = * 显示 = 动态 运算符 = GreaterThan 类型 = 日期 ValidationGroup = vgDA > ; < / asp:CompareValidator > ;







但不起作用..



每次给出错误,事件我输入结束时间大于开始时间,我使用24小时时间合成..





请为我提供解决方案,不仅仅是为了这两次验证有更好的想法,那么它也会得到鉴赏。

解决方案

进行客户端验证,即使用javascript验证time.split时间'小时','分钟'和'秒'然后比较每个。如果小时数相等则比较分钟,比如同样的时间也是明智的秒数。这仅适用于单日时间,即0.00至23.59。


在customvalidator中使用此功能
function timeToSec(str)
{
var h = Number(str.match(/ ^ \ d + /));
if(str.indexOf(PM)!= -1)
h + = 12;
var m = Number(str.match(/ ^ \d + :( \ d +):/)[1]);
var s = Number(str.match(/ :( \\\ +)\ s + [AP] M /)[1]);
返回(h * 60 + m)* 60 + s;
}
if(timeToSec(start)> timeToSec(end))
alert(Oops);


如果时间是下午12点,h将得到24.我添加了以下内容以确保12.00 pm(中午)仍然是12h

  if ((time.indexOf(  PM)!= -1)&&(h!=  12 ))
h + = 12


I have two textboxes with mask textbox attached for inputing time..

<asp:TextBox ID="txtStartTime" runat="server" MaxLength="8" CssClass="WellInfoLables"

     Width="150"></asp:TextBox>
<asp:MaskedEditExtender ID="MaskedEditExtender_txtStartTime" runat="server"  ClearMaskOnLostFocus="False" Mask="99:99" TargetControlID="txtStartTime" MaskType="Time" UserTimeFormat="TwentyFourHour"

MessageValidatorTip="true" />

///////And ////

<asp:TextBox ID="txtEndTime" runat="server" MaxLength="8" CssClass="WellInfoLables"

    Width="150"></asp:TextBox>
<asp:MaskedEditExtender ID="MaskedEditExtender_txtEndTime" runat="server" ClearMaskOnLostFocus="False"

   Mask="99:99" TargetControlID="txtEndTime" MaskType="Time" UserTimeFormat="TwentyFourHour"

   MessageValidatorTip="true" />



now additionaly i add one comparevalidation control to my page like this...

<asp:CompareValidator ID="ComValEndTime" runat="server" 

    ControlToValidate="txtEndTime" ControlToCompare="txtStartTime"

    ErrorMessage="End time is Greater then Start Time"

    Text="*" Display="Dynamic" Operator="GreaterThan" Type="Date" ValidationGroup="vgDA"></asp:CompareValidator>




but its not working..

its give error every time, event i enter the end time larger then start time, i use 24 hrs time formate..


please provide me the solution, not for this only if any better idea for validation this two time then it will also apprisiated.

解决方案

go for client side validation i.e. use javascript to validate the time.split the time by 'hours','minutes' and 'seconds' then compare each. if hours are equal compare minutes,like same
wise seconds also. this will work only single day time i.e. 0.00 to 23.59 only.


Use this funtion in customvalidator
function timeToSec(str) 
{   
var h = Number(str.match(/^\d+/));
   if(str.indexOf("PM") != -1)    
 h += 12;   
var m = Number(str.match(/^\d+:(\d+):/)[1]); 
  var s = Number(str.match(/:(\d+)\s+[AP]M/)[1]);  
 return (h * 60 + m) * 60 + s; 
} 
if(timeToSec(start) > timeToSec(end)) 
  alert("Oops"); 


If the hour is 12.00 pm, h will get 24. I added the following to ensure 12.00 pm (noon) remains 12h

if ((time.indexOf("PM") != -1) && (h != 12))
                h += 12;


这篇关于如何使用比较验证器验证时间..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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