文本框仅允许日期为DD/MM/YYYY的日期 [英] Text box allow only date in DD/MM/YYYY fromate
本文介绍了文本框仅允许日期为DD/MM/YYYY的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尊敬的会员,
我想检查文本框的验证,即文本框在c Sharp.仅允许DD/MM/YYYY格式.我得到了很多代码,但是这些代码无法正常工作.
请发布您的代码
谢谢你
代码项目成员
Dear Member,
i want to check validation on text box that text box allow only DD/MM/YYYY format in c sharp.i get many code but these code not working properly.
please post your code
thak you
member of code project
推荐答案
尝试此代码
try this code
<asp:textbox runat="server" id="TextBox1" xmlns:asp="#unknown" />
<asp:customvalidator runat="server" controltovalidate="TextBox1" errormessage="Date was in incorrect format" onservervalidate="CustomValidator1_ServerValidate" xmlns:asp="#unknown" />
背后的代码
Code Behind
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
DateTime d;
e.IsValid = DateTime.TryParseExact(e.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
}
/*If you want to allow other formats */
DateTime.TryParseExact(e.Value, new[] { "dd/MM/yyyy", "yyyy-MM-dd" }, CultureInfo.InvarinatCulture, DateTimeStyles.None, out d);
其他解决方案----------
Other Solution----------
public void Button1_Click(object sender, EventArgs e)
{
DateTime Test;
if (DateTime.TryParseExact(TextBox1.Text, "MM/dd/yyyy", null, DateTimeStyles.None, out Test) == true)
Response.Write("Date is Valid");
else
Response.Write("Invalid Date");
}
这篇关于文本框仅允许日期为DD/MM/YYYY的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文