dd / mm / yyyy格式问题的出生日期正则表达式验证 [英] date of birth regular expression validation for dd/mm/yyyy format issue

查看:120
本文介绍了dd / mm / yyyy格式问题的出生日期正则表达式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI all,



i我正在使用带有calander扩展器和正则表达式验证的文本框。



i有将日历日期格式设置为dd / MM / yyyy。现在当我在c#侧得到这个日期并将其转换为datetime时我得到的错误字符串无效为datetime。



这里是我的代码。



 <   asp:TextBox     ID   =  txtbirthdate    runat   = 服务器    CssClass   =  field   样式  =  width:290px; 
outline:none; padding-left:10px; height:25px;
MaxLength = 10 > < / asp:TextBox >
< asp:CalendarExtender OnClientShown = onCalendarShown ID = txtbirthdate_CalendarExtender

runat = server 已启用 = True TargetControlID = txtbirthdate 格式 = dd / MM / yyyy >
< / asp:CalendarExte nder >
< script type = text / javascript language = javascript >
function onCalendarShown(sender,args){
sender._switchMode( true );
}
< / 脚本 >
< asp:RequiredFieldValidator 显示 = 动态 ID = rvbirthdate 文本 = * 工具提示 = Pl easy输入Birthdate

runat = server ForeColor = 红色 Font-Bold = true ErrorMessage = ValidationGroup = loan

< span class =code-attribute> ControlToValidate = txtbirthdate > < / asp:RequiredFieldValidator >

< asp:RegularExpressionValidator ID = birthdatecheck runat = server ValidationGroup = loan



< span class =code-attribute> ErrorMessage = * 工具提示 = 请以dd / mm / yyyy格式输入日期 显示 = Dynam ic ForeColor = 红色 字体粗体 = true ControlToValidate = txtbirthdate





ValidationExpression = (0 [1-9] | [12] [0-9] | 3 [01])/(0 [1-9] | 1 [012])/ \d {4} > < / asp:RegularExpressionValidator >
< asp:TextBoxWatermarkExtender ID = txtbirthdate_TextBoxWatermarkExtender runat = server

< span class =code-attribute> 已启用 = WatermarkText = 出生日期:* TargetControlID = txtbirthdate >
< / asp: TextBoxWatermarkExtender >









和我的c#代码是



 objLoanApp.birthdate = Convert.ToDateTime(txtbirthdate.Text ); 

解决方案

请看我对这个问题的评论。



您只需要学习 System.DateTime 以及将字符串解析为日期数据的方法。请参阅名为 Parse ParseExact TryParse 的所有方法和 TryParseExact (并避免在大多数情况下使用转换;毕竟,这是解析,而不是转换) :

http:// msdn .microsoft.com / zh-CN / library / system.datetime%28v = vs.110%29.aspx [ ^ ]。



这些方法允许您从不同格式的字符串中解析时间,具体取决于格式字符串或当前或显式设置 culture 。您还需要学习如何使用格式字符串:

http://msdn.microsoft.com/en-us/library/az4se3k1(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110)。 aspx [ ^ ]。



-SA


这是因为IIS正在运行使用不同DateTime格式的不同语言环境。

您必须告诉解析器使用哪种格式。例如,

 System.Globalization.DateTimeFormatInfo dtf =  new  System.Globalization.DateTimeFormatInfo(); 
dtf.ShortDatePattern = dd / MM / yyyy;
string input = 20/11 / 1975\" ;
DateTime result = DateTime.Parse(input,dtf);


 objLoanApp.birthdate = DateTime.ParseExact(txtbirthdate。文本, 日/月/年,NULL)


HI all,

i am using textbox with calander extender and regular expression validation.

i have set calender date format to dd/MM/yyyy. now when i get this date on c# side and convert that to datetime i am getting error string is not valid as datetime.

here is my code.

<asp:TextBox ID="txtbirthdate" runat="server" CssClass="field" Style="width: 290px;
                                        outline: none; padding-left: 10px; height: 25px;" MaxLength="10"></asp:TextBox>
                                    <asp:CalendarExtender OnClientShown="onCalendarShown" ID="txtbirthdate_CalendarExtender"

                                        runat="server" Enabled="True" TargetControlID="txtbirthdate" Format="dd/MM/yyyy">
                                    </asp:CalendarExtender>
                                    <script type="text/javascript" language="javascript">
                                        function onCalendarShown(sender, args) {
                                            sender._switchMode("years", true);
                                        }
                                    </script>
                                    <asp:RequiredFieldValidator Display="Dynamic" ID="rvbirthdate" Text="*" ToolTip="Please Enter Birthdate"

                                        runat="server" ForeColor="Red" Font-Bold="true" ErrorMessage="" ValidationGroup="loan"

                                        ControlToValidate="txtbirthdate"></asp:RequiredFieldValidator>
                                 
                                        <asp:RegularExpressionValidator ID="birthdatecheck" runat="server" ValidationGroup="loan"



             ErrorMessage="*" Tooltip="Please enter date in dd/mm/yyyy format" Display="Dynamic" ForeColor="Red" Font-Bold="true"   ControlToValidate="txtbirthdate"





            ValidationExpression="(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/\d{4}"></asp:RegularExpressionValidator> 
                                    <asp:TextBoxWatermarkExtender ID="txtbirthdate_TextBoxWatermarkExtender" runat="server"

                                        Enabled="True" WatermarkText="Date of Birth: *" TargetControlID="txtbirthdate">
                                    </asp:TextBoxWatermarkExtender>





and my c# code is

objLoanApp.birthdate = Convert.ToDateTime(txtbirthdate.Text);

解决方案

Please see my comment to the question.

You just need to learn System.DateTime and methods of parsing of string into date data. Please see all the methods named Parse, ParseExact, TryParse and TryParseExact (and avoid using Convert, in most cases; after all, this is parsing, not "conversion"):
http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^].

These methods allow you do parse time from string in different formats, depending on format string or current or explicitly set culture. You also need to learn how to use format strings:
http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx[^].

—SA


That happens because IIS is running with a different locale which uses a different DateTime format.
You must tell the parser which format to use. E.g.

System.Globalization.DateTimeFormatInfo dtf = new System.Globalization.DateTimeFormatInfo();
dtf.ShortDatePattern = "dd/MM/yyyy";
string input = "20/11/1975";
DateTime result = DateTime.Parse(input, dtf);


objLoanApp.birthdate = DateTime.ParseExact(txtbirthdate.Text,"dd/MM/yyyy",null)


这篇关于dd / mm / yyyy格式问题的出生日期正则表达式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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