Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All, have a good day.

How to Validate Textbox with Previous Year.
==========================================

I have a Textbox on my webform, in that user must enter only Next year dates(upcoming years date) like 2013 , 2014,2015 like this.

He should not enter 2007,2008,2009....if he enter like this previous year..,a message must be displayed ."Please enter Valid date"


Please give me a good solution,

Thank you sir.
Posted

Hi ,
Check this solution , Just use CompareValidator ValueToCompare="2012" Operator="GreaterThan" only this proprieties you have to care about to cause the validation and ErrorMessage to Give the Error message you want .

HTML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:CompareValidator ID="CompareValidator1" runat="server"
          ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="invalid year........ "
          Operator="GreaterThan" Type="Integer" ValueToCompare="2012"></asp:CompareValidator>
 
Share this answer
 
v3
Comments
Software Engineer 892 16-Jul-12 5:13am    
if the year is less than current year , it must display a message invalid year

Pleaseeeeeeeeeeeeee help.
Mohamed Mitwalli 16-Jul-12 6:46am    
ErrorMessage="Put you Error Message here"
You could create a DateRange validator like this:

C#
[AttributeUsage(AttributeTargets.Property)]
   public class DateIsNextYear : ValidationAttribute
   {
       public DateIsNextYear(string errorMessage = "")
       {
           ErrorMessage = errorMessage;
       }

       protected override ValidationResult IsValid(object value, ValidationContext validationContext)
       {
           if ((value == null) || (string.IsNullOrEmpty(value.ToString())))
               return null;

           try
           {
               var date = DateTime.Parse(value.ToString());
               var nextyear = DateTime.Now.Year + 1;
               if ((date >= new DateTime(nextyear , 1, 1)) && (date <= new DateTime(nextyear , 12, 31)))
                   return ValidationResult.Success;
               return new ValidationResult(ErrorMessage);
           }
           catch
           {
               return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
           }
       }
   }



And use it for yourobject:

C#
public class MyObject
    {
        [DateIsNextYear("This date is not next year!")]
        public DateTime DateToFillIn { get; set; }
    }


You can easily modify this to validate only dates higher than today etc..
 
Share this answer
 
v6
There can be various ways. Simplest would be to have a Javascript validation for it. Try something like:
HTML
<asp:Textbox ID="myTextbox" runat="server" />
<asp:Button ID="Submit Value" runat="server" OnClientClick="ValidateYear();" />

JavaScript
function ValidateYear()
{
  var d = new Date(); // This will get you current date
  var currentYear = d.getFullYear();
  
  var currentValue = document.getElementById('<%= myTextbox.ClientID %>').value;

  if(parseInt(currentValue) <= parseInt(currentYear))
  {
     alert("Please enter valid date!");
     return false;
  }
  return true;
}

Try!

P.S.: There might be few validations you need to take care of. This is a raw implementation.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900