最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - validate textbox for date of birth - Stack Overflow

programmeradmin2浏览0评论

The below code is for validating a textbox for date of birth. The conditions are

  1. Textbox can't be empty
  2. Textbox date should be in format dd/mm/yyyy
  3. Textbox date should not be larger than current date. ie; no future date (to show error like -U r not born yet dude)

<asp:TextBox ID="txtDateOfBirth" runat="server" CausesValidation="True" />
          (dd/mm/yyyy e.g. : 12/12/2011)
<asp:CustomValidator runat="server" ID="valDateRange" ControlToValidate="txtDateOfBirth" ErrorMessage="enter valid date" />

But the problem is that, the textbox is inside an ajax wrapper so only client side validations will work. Anybody here, Plz help me with any hints, suggestions or with working code!!. I will be very great-full because i was working on this since morning

The below code is for validating a textbox for date of birth. The conditions are

  1. Textbox can't be empty
  2. Textbox date should be in format dd/mm/yyyy
  3. Textbox date should not be larger than current date. ie; no future date (to show error like -U r not born yet dude)

<asp:TextBox ID="txtDateOfBirth" runat="server" CausesValidation="True" />
          (dd/mm/yyyy e.g. : 12/12/2011)
<asp:CustomValidator runat="server" ID="valDateRange" ControlToValidate="txtDateOfBirth" ErrorMessage="enter valid date" />

But the problem is that, the textbox is inside an ajax wrapper so only client side validations will work. Anybody here, Plz help me with any hints, suggestions or with working code!!. I will be very great-full because i was working on this since morning

Share Improve this question edited Feb 20, 2012 at 12:12 Kiran George asked Feb 20, 2012 at 12:02 Kiran GeorgeKiran George 1012 silver badges10 bronze badges 2
  • possible duplicate of Javascript Date validation for mm/dd/yyyy format in asp – outis Commented Feb 20, 2012 at 12:05
  • use jquery datepicker plugin. it can be customize as per your need.. also refer stackoverflow./questions/269545/… jqueryui./demos/datepicker – Hemang Rami Commented Feb 20, 2012 at 12:09
Add a ment  | 

4 Answers 4

Reset to default 3

You could have a ClientValidationFunction property on your CustomValidator...

<asp:CustomValidator runat="server" ID="valDateRange" ControlToValidate="txtDateOfBirth" ErrorMessage="enter valid date" ClientValidationFunction="validateDate" />

Then create a Javascript function:

function validateDate(sender, e) {

    // Split out the constituent parts (dd/mm/yyyy)    
    var dayfield = e.Value.split("/")[0];
    var monthfield = e.Value.split("/")[1];
    var yearfield = e.Value.split("/")[2];

    // Create a new date object based on the separate parts
    var dateValue = new Date(yearfield, monthfield - 1, dayfield)

    // Check that the date object's parts match the split out parts from the original string
    if ((dateValue.getMonth() + 1 != monthfield) || (dateValue.getDate() != dayfield) || (dateValue.getFullYear() != yearfield)) {
        e.IsValid = false;
    }

    // Check for future dates
    if (e.IsValid) {
        e.IsValid = dateValue <= new Date()
    }
}

I agree with varangian_12's answer, but be sure to also do some sort of server-side validation for the off-case that your user has Javascript disabled, or disables it to get around your validation

You could do a simple DateTime.TryParse([string value]) and then check to ensure the date occurs in the past

You just need to be sure to handle the edge-case scenarios

No need to use valodators, use requiredfieldvalidator for checking if not empty and use jquery date picker to make the field have a date calendar when get focus,

here is an example it is very easy, if u need any help please let me know: http://jqueryui./demos/datepicker/

You should try this .i will run perfect. you can also used "jquery.validate.js" for make Textbox can't be empty. also in css you have to create a class error

having color red.

 <p>   type="text/css" media="all" /></p>
<p><script type="text/javascript" src="http://code.jquery./jquery-1.4.4.min.js"></script></p>

   <p>   type="text/javascript"></script></p>
 <p> <script type="text/javascript"></p>
<p>    $(function () {</p>
  <p>        var minDate = new Date('1/1/1990');</p>
     <p>    var todaysDate = new Date();</p>
     <p>    var maxDate = new Date(todaysDate.getFullYear(),</p>
        <p>                    todaysDate.getMonth(),</p>
             <p>               todaysDate.getDate() - 1);</p>
       <p>  var currentsYear = todaysDate.getFullYear();</p>

        <p> var range = '1900:' + currentsYear</p>
    <p>      $('#txtDOB').datepicker({</p>
     <p>         minDate: minDate,</p>
       <p>       maxDate: maxDate,</p>
       <p>       changeMonth: true,</p>
       <p>       changeYear: true,</p>
       <p>       yearRange: range</p>
      <p>    });</p>
  <p>    });  </p>

发布评论

评论列表(0)

  1. 暂无评论