Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date validation

how we used data validation on asp.net? date can't be insert greater than the current date.

like image 611
user389248 Avatar asked Jul 12 '10 07:07

user389248


People also ask

How can we validate date?

Among various kind of data validation, validation of date is one. 1. dd/mm/yyyy or dd-mm-yyyy format.

What is validation in date of birth?

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.


2 Answers

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.

like image 69
Daniel Dyson Avatar answered Oct 31 '22 20:10

Daniel Dyson


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");
  }
like image 39
Pranay Rana Avatar answered Oct 31 '22 20:10

Pranay Rana