Results 1 to 2 of 2

Thread: Validate a Date

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    Validate a Date

    I have three drop down list boxes that allow the user to provide a date and time that will be added to a database. I want to assure that the date and time occur after the present date and time.

    I tried using a custom validator, and the error message does appear if the date selected is not after the current date, but for some reason it still allows the information to be processed into the database.

    Any idea what I am doing wrong here? Why does the information get processed even when I am setting the isvalid property to false?

    private void rfDate_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
    {
    DateTime dt = System.DateTime.Now;
    DateTime re = new DateTime(Convert.ToInt32(ddYear.SelectedItem.ToString()),ddMonth.SelectedIndex+1,ddDay.SelectedIndex +1,ddHour.SelectedIndex+1,0,0,0);

    //determine if the date selected is greater than right now
    if (re > dt)
    args.IsValid = true;
    else
    args.IsValid = false;
    }

  2. #2
    Addicted Member rdove's Avatar
    Join Date
    Dec 2002
    Location
    Indianapolis
    Posts
    251
    I'm not positive on how to do this in C# (or C++), but when I use the custom validator in VB.NET I have to also put client side validation in the HTML portion of the code.

    It looks something like your code:

    Code:
    if (re > dt) 
    args.IsValid = true; 
    else 
    args.IsValid = false;
    Anyway, I hope this is somewhat helpful to you.
    ~Ryan





    Have I helped you? Please Rate my posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width