views:

84

answers:

4
+7  Q: 

date validation

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

+1  A: 

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");
  }
Pranay Rana
+2  A: 

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.

Daniel Dyson
Tagging the question with a technology suggested in your answer is a bit rude...
ck
Sorry, is it considered rude? I thought it was helpful. That way the user can easily find other questions on that technology. Also, other people who use that technology can find this question and provide help. I thought I was being helpful. Is there a community post on what is considered rude? I am happy to remove the tags if other users mark @ck's comment as a great comment. Probably better to do that than add your own comment as that will be off the topic of this question.
Daniel Dyson
+1  A: 

In javascript solution,

Make sure to set hrs, min, secs, and milliseconds to 0, if you just want to compare dates (i.e day,month and year). Function to acheive the above mentioned is as follows,

function f_tcalResetTime (d_date) {
    d_date.setHours(0);
    d_date.setMinutes(0);
    d_date.setSeconds(0);
    d_date.setMilliseconds(0);
    return d_date;
}

If you compare dates, javascript actually call date.valueOf function behind the scenes, which returns the number of millisecond passed since midnight January 1, 1970.

Vinod T. Patil
A: 

Hello, good date mate. There are many options available. You can either use asp.net's custom validator control, javascript etc. A better option would be to use AJAX control toolkit's masked edit extender with masked edit validator. You can specify ranges, custom mask (yyyy/MM/dd) and even an empty field message with that.

Just make sure you set the culture of your website right so that you can validate dates properly. http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/MaskedEdit/MaskedEdit.aspx

Cheers.

<ajaxToolkit:MaskedEditValidator
ControlExtender="MaskedEditExtender2"
ControlToValidate="TextBox2" 
IsValidEmpty="False" 
MaximumValue="12000" 
EmptyValueMessage="Number is required"
InvalidValueMessage="Number is invalid"
MaximumValueMessage="Number > 12000"
MinimumValueMessage="Number < -100"
MinimumValue="-100" 
EmptyValueBlurredText="*" 
InvalidValueBlurredMessage="*" 
MaximumValueBlurredMessage="*" 
MinimumValueBlurredText="*"
Display="Dynamic" 
TooltipMessage="Input a number: -100 up to 12.000"/>
Popo