简体   繁体   中英

How to validate a date of birth with a date of deceased textboxes using Jquery.ui.datepicker.css

I have a text box that I believe is referencing Jquery.ui.datepicker.css via CssClass? :

Textbox 1- Date of birth (where all the validation takes place):

<asp:TextBox runat="server" ID="txtDOB" CssClass="small date"></asp:TextBox>

Textbox 2- Date of deceased (where data needs to be pulled from for validation to take place):

<asp:TextBox runat="server" ID="txtDOD" CssClass="small date"></asp:TextBox>

I'm unfamiliar with jquery but have been trying to program against these two textboxes using C# is this possible?

I'm also unfamiliar with this class jquery.ui.datepicker.css where does it come from? how is it referenced exactly? and why is this chosen over your typical asp.net control?

If not how can I write jquery expressions (would I need regular expressions?) that will accomplish the requirements?

I am trying to accomplish meeting these requirements: It cannot be a future date, and must be less than the date deceased and must be at least 18 years ago.

Thank you for any guidance you may have to offer.

jQuery UI Date Picker just adds a date picker to a text box. When you click in the text box, a little calendar appears and you can pick the date you want. It makes it easier than typing the date in in some cases. However, it's probably irrelevant to your question.

You need to validate the data on the server side to make sure you don't get bad data, but it's a good idea to also validate on the client side to avoid a round trip to the server. What you need to do (on both server and client) is...

  1. Get the value of the date of birth control
  2. Get the value of the date of deceased control
  3. Compare the values
  4. If they don't make sense, provide an error message to the user telling them to correct the problem.

Server side example:

DateTime dob=DateTime.Parse(txtDOB.Text); //consider using an overloaded method to account for different date formats.
DateTime dod=DateTime.Parse(txtDOD.Text); //consider using an overloaded method to account for different date formats.
if(dob>dod)
{
    //provide error message to the client
}

Ideally, this client side logic should happen in your model layer and return some sort of validation summary object to the UI layer, which the UI layer is then responsible for figuring out how to display it to the client.

I'll leave the client side example to others. Note that you'll want to set the ClientIdMode to static so that you get the correct ID's in your controls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM