date validation
Asked Answered
O

4

6

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

Oldfashioned answered 12/7, 2010 at 7:15 Comment(0)
P
2

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.

Piccalilli answered 12/7, 2010 at 7:35 Comment(1)
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.Piccalilli
T
1

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");
  }
Tegular answered 12/7, 2010 at 7:16 Comment(0)
L
1

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.

Liquid answered 12/7, 2010 at 13:50 Comment(0)
P
0

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"/>
Pipsissewa answered 12/7, 2010 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.