简体   繁体   English

如何在一个代码中同时检查无效的日期格式和18岁以上的年龄验证?

[英]How to check both invalid date format and age greater than 18 years validation in one code?

I have written javascript like below 我写了下面的JavaScript

    <script type="text/javascript">
    function ValidateDate() {
        var dt = document.getElementById("<%=txtDOB.ClientID%>").value;
        var currentTime = new Date();
        var inputdate = dt;
        currentTime = currentTime.format('dd-MMM-yyyy');
        if (dt == "" || dt == undefined) {
            document.getElementById("<%=lblValidDate.ClientID%>").style.display = "none";
        }
        else {  
        var inputdate = dt;
            if (new Date(inputdate).getYear() <= new Date(currentTime).getYear() - 18) {

                document.getElementById("<%=lblValidDate.ClientID%>").style.display = "none";
                return true;
            }
            else if(new Date(inputdate).getYear() >= new Date(currentTime).getYear() - 18)
            {

                    document.getElementById("<%=lblValidDate.ClientID%>").style.display = "block";
                    return false;
                }

        else {
                document.getElementById("<%=lblValidDate.ClientID%>").style.display = "none";
                return false;

            }

        }

    }
</script>

In html 在html中

<div style="display:none" id="dateformaterror">Please enter date in    mm/dd/yyyy or m/d/yyyy format</div>
   <asp:Label runat="server" ID="lblValidDate" Style="color: Red; display: none;"><b>Minimum Age should be 18 years old</b></asp:Label>
  <asp:TextBox ID="txtDOB" CssClass="datepiker" runat="server" onchange="ValidateDate()"></asp:TextBox>

I want that the above javascript should check both validation. 我希望上面的javascript应该检查两个验证。 one is that the age should be above 18 years and the second one is if user enters date in wrong format I mean other than mm/dd/yyyy or m/d/yyyy format then the div with class datefomaterror should be visible? 一种是年龄应该在18岁以上,第二种是如果用户以错误的格式输入日期,我的意思是除mm / dd / yyyy或m / d / yyyy格式以外,则类datefomaterror的div应该可见?

Please help me!!! 请帮我!!! Also guide me in improving the javascript code. 还要指导我改善javascript代码。

var start ="1989-01-10";

// ex) 2014-08-10
if (start.length != 10 || start.indexOf("-") < 0) {
    alert("[Error] Check date form");
    return null;
}
// get Age
var nowDate = new Date();
var birth = new Date(start);

var now = nowDate.getFullYear();
var my =birth.getFullYear();
var myAge = now - my -1;

if(myAge < 18) {
    alert("You are so young");
}
else if (myAge <0) {
    alert("Please,Try again");
}
else
    alert("You are "+ myAge);

One way of confirming the validity of a date is using the valueOf() method in Date . 确认日期有效性的一种方法是使用DatevalueOf()方法。

example: 例:

    var a = new Date('12/15/2015');
    isNaN(a.valueOf()); // false, this means it is a valid date.

    // whereas an invalid date would return true
    a = new Date('14/15/2015');
    isNaN(a.valueOf()); // true

You can use this to check if the date entered is valid of not. 您可以使用它来检查输入的日期是否有效。 isNan checks if a number is Not a Number . isNan检查数字是否Not a Number

So your ValidateDate() method can be something like this: 因此,您的ValidateDate()方法可以如下所示:

    function ValidateDate() {
        var inputDateString = document.getElementById('date').value,
            inputDate = new Date(inputDateString),
            invalidErrorMessage =  document.getElementById('invalid'), // Replace with your ID
            below18ErrorMessage = document.getElementById('below18'); // Replace with your ID

        invalidErrorMessage.style.display = 'none';
        below18ErrorMessage.style.display = 'none';

        if (!inputDateString.trim() || isNaN(inputDate.valueOf()) {
           invalidErrorMessage.style.display = "block";
           return;
        }

        if (new Date().getFullYear() - inputDate.getFullYear() < 18) {
            below18ErrorMessage.style.display = "block";
            return;
        }
    }

Please note in this check - and / both give valid dates. 请注意此支票-/均提供有效日期。

Also here 也在这里

    var currentTime = new Date();
    currentTime = currentTime.format('dd-MMM-yyyy');

Date does not have a format() method, unless you have changed its prototype . 除非您更改了prototype ,否则Date没有format()方法。

Use this fiddle here for more reference. 在此处使用此小提琴以获得更多参考。

You can try Moment.js to fiddle with dates - http://momentjs.com/ 你可以尝试用Moment.js日期捣鼓- http://momentjs.com/

Also check this out: Moment.js - how do I get the number of years since a date, not rounded up? 还请检查以下内容: Moment.js-如何获取自日期起的年数,而不是四舍五入?

暂无
暂无

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

相关问题 检查日期验证是否大于今天的日期以及js中的日期格式 - check date validation with greater than today's date along with date format in js 在javascript中检查年龄是否不低于13岁 - To check if age is not less than 13 years in javascript PHP Datepicker 验证年龄是否低于 18 岁 - PHP Datepicker Validate if age is lower than 18 years old 如何检查变量中是否有一个大于0 - How to check if any one of the variable is greater than 0 如何检查一个值是否大于另一个 - How to check if one value is greater than other 我需要限制从当前日期起在 PHP 中低于 18 岁的年龄 - I need to restrict age for below 18 years age from the current date in Php 如何检查一个文本框的值是否应小于另一个文本框的值,以及两个值的总和应不大于1 - How to check if one textbox value should be less than another textbox value and also the sum of both values should not be greater than 1 如何检查条件大于或小于javascript中的日期? - How to check condition for date greater than or smaller than in javascript? jQuery验证-日期规则,以检查所选日期是否比当前日期大5天 - Jquery validation - date rules to check if selected date is 5 days greater than current date 如何检查 arrays 的数组以查看内部数组的两个值是否都大于前一个的值? - How can I check an array of arrays to see if an internal array has both values greater than the values of the previous one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM