简体   繁体   English

验证文本框的出生日期

[英]validate textbox for date of birth

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 文本框日期应采用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) 没有未来的日期(以显示类似-U r尚未出生的消息的错误)

<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. 但是问题在于,文本框位于ajax包装器内,因此只有客户端验证才可以使用。 Anybody here, Plz help me with any hints, suggestions or with working code!!. 这里的任何人,Plz都可以通过任何提示,建议或工作代码来帮助我!! I will be very great-full because i was working on this since morning 我会很饱的,因为从早上开始我一直在做这个

You could have a ClientValidationFunction property on your CustomValidator... 您可以在CustomValidator上拥有一个ClientValidationFunction属性...

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

Then create a Javascript function: 然后创建一个Javascript函数:

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 我同意varangian_12的回答,但请确保还针对用户禁用Java的情况下进行某种服务器端验证,或者禁用它来避开验证

You could do a simple DateTime.TryParse([string value]) and then check to ensure the date occurs in the past 您可以执行一个简单的DateTime.TryParse([string value]) ,然后检查以确保日期在过去

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, 无需使用valodators,使用requiredfieldvalidator检查是否不为空,并使用jquery日期选择器使该字段在获得焦点时具有日期日历,

here is an example it is very easy, if u need any help please let me know: http://jqueryui.com/demos/datepicker/ 这是一个非常简单的示例,如果您需要任何帮助,请告诉我: http : //jqueryui.com/demos/datepicker/

You should try this .i will run perfect. 您应该尝试一下。我会完美运行。 you can also used "jquery.validate.js" for make Textbox can't be empty. 您还可以使用“ jquery.validate.js”来使文本框不能为空。 also in css you have to create a class error 另外在CSS中,您必须创建一个类错误

having color red. 具有红色。

 <p>   type="text/css" media="all" /></p>
<p><script type="text/javascript" src="http://code.jquery.com/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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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