Click here to Skip to main content
15,886,714 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
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.NET
<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

C#
objLoanApp.birthdate = Convert.ToDateTime(txtbirthdate.Text);
Posted
Comments
Sergey Alexandrovich Kryukov 20-Feb-14 23:30pm    
What is "calender"?!

Now, why Regex? If you do validation of server side, System.DateTime does it all.

—SA
phil.o 7-Mar-15 7:56am    
Regular expressions are not meant to validate datetimes; at all. Why? Because datetimes are not only a collection of digits and symbols, there are some rules that cannot be enforced in regular expressions (leap years, the number of days in a given month, etc...). As it was told to you, System.DateTime already has required methods to do this validation.

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
 
Share this answer
 
v2
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.
C#
System.Globalization.DateTimeFormatInfo dtf = new System.Globalization.DateTimeFormatInfo();
dtf.ShortDatePattern = "dd/MM/yyyy";
string input = "20/11/1975";
DateTime result = DateTime.Parse(input, dtf);
 
Share this answer
 
objLoanApp.birthdate = DateTime.ParseExact(txtbirthdate.Text,"dd/MM/yyyy",null)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900