how we used data validation on asp.net? date can't be insert greater than the current date.
Among various kind of data validation, validation of date is one. 1. dd/mm/yyyy or dd-mm-yyyy format.
Date format dd/MM/yyyy validation: The Date of Birth (DOB) will be first validated for dd/MM/yyyy format using Regular Expression (Regex). 2. 18+ Minimum Age validation: The difference between the age entered in the TextBox and the Current Date is minimum 18 years.
Use a CompareValidator. Most people use this for comparing two values entered into two textboxes, but you can also use it to compare one entered value with a set value as in your case.
<asp:CompareValidator id="Compare1"
ControlToValidate="TextBox1"
Type="Date"
runat="server"/>
In the code behind set Compare1.ValueToCompare = new DateTime(...);
and Compare1.Operator = ValidationCompareOperator.LessThanEqual;
Also, Remember: You should always validate on the Server as well as the client, because clientside validation is easy to switch off or by-passed. I would suggest you look at Fluent validation for this.
Make use of the CustomValidator
will resolve your issues easily.
CustomValidator
or
You can use javascript to validate your date like as following
var myDate=new Date();
myDate.setFullYear(2010,0,14);
var today = new Date();
if (myDate>today)
{
alert("Today is before 14th January 2010");
}
else
{
alert("Today is after 14th January 2010");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With