How can i validate date? - asp.net

In a page i have taken a textbox used to enter date in mm/dd/yyyy formate.
I want that when user enter the incorrect format of the date then it show a message that incorrect format of date.
How can i validate textbox that only correct fomate of date can be entered by user.
Thanks in advance..

Instead of validating use calender control or there are loads of Jquery calenders, google it , Its better to provide user with date selection instead of date insertion. Make it idiot proof.

If Not IsDate(txtDate.Text) Then
'Error message code here...
End If

You could also use Date.TryParse().

Use DateTime.TryParseExact method.
DateTime dateValue;
if (DateTime.TryParseExact(textBox.Text, "mm/dd/yyyy",
CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
{
}

Related

How can i Convert the DatePicker Input to string

I am using the following Codes to get the date from the date picker
objDD_ml.EffDate=(DirectCast(drv.Cells(3).FindControl("rdpEffDate"), RadDatePicker).DateInput).ToString
But it does not return the date... It returns like 'DateInput'
How can i use the code to get the value for Effdate as a correct format of date
Anyone please help me..
Whenever you want date into string and in specific format you can always apply following :
objDD_ml.EffDate = (
DirectCast(drv.Cells(3).FindControl("rdpEffDate"), RadDatePicker).DateInput
).ToString("dd/MM/yyyy")
Try to cast the FindControl object into the corresponding ASP control externally. Then access the value of that control and convert it to .ToString
I don't know what control you are using, but it should be something like below -
objDD_ml.EffDate = ((RadDatePicker)(drv.Cells[i].FindControl("rdpEffDate")).SelectedDate.Tostring;

Error converting date for Calendar in asp.net

I have written code for two checkboxes:
string sdate= Convert.ToDateTime(txtFromDate.Value);
string edate=Convert.ToDateTime(txtEndDate.Value);
I am getting the following error: "String was not recognized as a valid DateTime".
Well, it's reasonably clear: the input string wasn't in an appropriate format. I suggest that instead of using Convert.ToDateTime, you use DateTime.TryParseExact and specify the format string (or strings) that you expect the user to enter.
Using TryParseExact instead of just ParseExact means you can detect if the user has entered an incorrect date without an exception being thrown - and you should check for that. Basically the return value of TryParseExact indicates success or failure, and an out parameter is used to capture the parsed date/time on success.
What I hadn't noticed to start with is that you're then trying to assign a DateTime value to a string. That's not going to work - but we can't advise you on what you should be doing instead without knowing what you want to do with the data. I suspect you want to change sdate and edate to be DateTime variables instead.

invalid date syntax with ajax calendar

I had Ajax calendar and when I selected from it it give me date as (07/12/2010)
and when I made space between the year and / it converted to (2010/07/12) and this invalid date so pleas what the error.
That's not a error is it? It sounds to me like the Ajax is just setting the date as a culture invariant. See this article on CodeProject
Try Specifying the date Format by using calendar Format property to "dd/MM/yyyy"
http://www.ajaxlines.com/ajax/stuff/article/aspnet_ajax_calendarextender_and_validation.php
Check out http://www.ama3.com/anytime/
If you are working from JS you would be better off working from its numeric (epoch) representation and then convert it to whatever format you want to use or use the js date api to achieve what you want.
https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date

ASP.NET [How to get current Date from Windows and put into textbox]

How to get current Date from Windows and put into my textbox
For just the date use:
someTextBox.Text = DateTime.Now.ToShortDateString();
For date and time use:
someTextBox.Text = DateTime.Now.ToString();
You might also want to look at the custom and standard formats available to you.

The string was not recognized as a valid datetime

objfile.dateFileDate=convert.ToDatetime(Format(txtdate.text,"MM/dd/yyyy hh:mm"))
following error is coming
The string was not recognized as a
valid datetime .There is an unknown
word starting at 0.
What should i do to save this datetime,
please help
You can't format normal text using datetime formats.
Try
C#
objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", null);
VB.NET
objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", Nothing)
This is assuming dateFileDate is a DateTime type and that the txtdate.text is in the above format.
If your program is used by a international crowd, read on :)
ppl from different cultures will write dates in diffrent formats, so if your always going to parse the string that could be get sticky. Consider using the calander control?
Im saying this based on personal experience.
Also finding out why your current one is failing, i would do a DateTime.Now.ToString() and compare that to whats in the textbox so you can see whats curently being typed in wrong ( While debugging off course, to help track down the problem)
Try hh:nn instead of hh:mm
I believe mm is Months in two digit format and nn is minutes in two digit format.

Resources