文本框文本接受时间格式 [英] Textbox text to accept Time format
本文介绍了文本框文本接受时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何设置文本框文本以仅接受时间格式的值.
谢谢
Hi,
how to set a text box text to accept values in time format only.
Thanks
推荐答案
为什么不使用ajax日历控件?
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx [ ^ ]
或使用正则表达式验证器:
http://msdn.microsoft.com/en-us/library/ff650303.aspx [ ^ ]
在此处查找日期的正则表达式:
http://regexlib.com/Search.aspx?k=Date%20validation [ ^ ]
why not use ajax calender control?
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx[^]
or use a regular expression validator:
http://msdn.microsoft.com/en-us/library/ff650303.aspx[^]
Look here for regular expression for date:
http://regexlib.com/Search.aspx?k=Date%20validation[^]
用于日期验证的自定义验证或用于以正确格式输入时间的正则表达式验证器,您可以编写用于时间验证的Java脚本,该脚本与coutom验证绑定:
you use the custom validation for date validation or regularexpression validator for enter time in correct form you write java script for time validation which bind with coutom validation:
<pre lang="cs"><script type="text/javascript">
function validTime(source,args)
{
var res=false;
var orgVAlue=args.Value;
var hour=orgVAlue.substring(0,2);
var min=orgVAlue.substring(3,5);
var sec=orgVAlue.substring(6,8);
if(orgVAlue.length==0)
res=false;
else if(orgVAlue.length>8)
res=false;
else
{
if((hour>23)||(min>59)||(sec>59))
res=false;
else
res=true;
}
args.IsValid=res;
}
</script>
现在我使用文本框,如:-
now i use with text box like:-
<pre lang="xml"><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="validTime"
ControlToValidate="TextBox1" ErrorMessage="Not valid date" SetFocusOnError="True"
ValidateEmptyText="True"></asp:CustomValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Enter date in dd:mm:ss" SetFocusOnError="True" ValidationExpression="dd:dd:dd"></asp:RegularExpressionValidator><br />
<asp:Button ID="Button1" runat="server" Text="Button" />
现在,您可以尝试上面的代码.
Now you try this above code.
验证器将在用户键入错误内容时告诉用户.要实际上阻止他们键入内容,您需要编写javascript以便在键入内容时对其进行解析.时间的一个更好的主意是使下拉菜单持续数小时和数分钟.
A validator will tell the user when they typed something wrong. To actually stop them typing it, you need to write javascript to parse input as they type. A better idea for time is to have drop downs for hours and for minutes.
这篇关于文本框文本接受时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文