English 中文(简体)
js 过期日期 如何使用此校验添加方法
原标题:jquery.validate.js expiration date how to use this validator add method

我有一张信用卡处理表,其中有两个过期日期的选定字段。像这样

<select name="qbms_cc_expires_month" id="qbms_cc_expires_month"><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option></select><span class="required">*</span>&nbsp;<select name="qbms_cc_expires_year" id="qbms_cc_expires_year"><option value="12">2012</option><option value="13">2013</option><option value="14">2014</option><option value="15">2015</option><option value="16">2016</option><option value="17">2017</option><option value="18">2018</option><option value="19">2019</option><option value="20">2020</option><option value="21">2021</option></select>

我加了这个美元校验器 addMethod

    $.validator.addMethod(
        "ccexpdate",
    function (value, element) {

        // Initialize todays date   i.e start date
        var today = new Date();
        var startDate = new Date(today.getFullYear(),today.getMonth(),1,0,0,0,0);

        // Initialize End/Expiry date i.e. adding 10 years to expire
        var futureLimitDate= new Date(today.getFullYear()+10,today.getMonth(),1,0,0,0,0);
        var expDate = value;

        var expYearCheck=  ;

        // Check Date format
        var separatorIndex = expDate.indexOf( / );
        if(separatorIndex==-1)return false; // Return false if no / found

        var expDateArr=expDate.split( / ); 
        if(expDateArr.length>2)return false; // Return false if no num/num format found

        // Check Month for validity
        if(eval(expDateArr[0])<1||eval(expDateArr[0])>12)
        {
            //If month is not valid i.e not in range 1-12
            return false;
        }

        //Check Year for format YY or YYYY
        switch(expDateArr[1].length)
        {
            case 2:expYearCheck=2000+parseInt(expDateArr[1], 10);break; // If YY format convert it to 20YY to it
            case 4:expYearCheck=expDateArr[1];break; // If YYYY format assign it to check Year Var
            default:return false;break;
        }


        // Calculated new exp Date for ja  
        expDate=new Date(eval(expYearCheck),(eval(expDateArr[0])-1),1,0,0,0,0);



        if(Date.parse(startDate) <= Date.parse(expDate))
        {
            if(Date.parse(expDate) <= Date.parse(futureLimitDate))
            {
                // Date validated
                return true;    
            }else
            {

                // Date exceeds future date
                return false;
            }

        }else
        {

            // Date is earlier than todays date
            return false;
        }


    },
    "<br />Must be a valid Expiration Date."
    );

How do I call this in my rules: { } section? I tried this qbms_cc_expires_year: { ccexpdate: { month: #qbms_cc_expires_month , year: #qbms_cc_expires_year } }, But it doesn t work.

问题回答

我找到了这个方法, 希望它能帮助某人完成同样的事情。 我的问题正试图使用 jquery. validate. js 和下面的 $. validator.addMethod 。 我有两个字段, 我的到期日期为 MM "01", 而另一个是 YYYY 的到期日期为 YYYYY 。 < 选择值=12 & gt; 2012</option> " , 所以我把它们合并成另一个文本输入, 成为 MM/ YYYY , 并且对这个字段进行了验证 。

$.validator.addMethod(
        "ccexpdate",
    function (value, element) {
        // Initialize todays date   i.e start date
        var today = new Date();
        var startDate = new Date(today.getFullYear(),today.getMonth(),1,0,0,0,0);
        // Initialize End/Expiry date i.e. adding 10 years to expire
        var futureLimitDate= new Date(today.getFullYear()+10,today.getMonth(),1,0,0,0,0);
        var expDate = value;
        var expYearCheck=  ;
        // Check Date format
        var separatorIndex = expDate.indexOf( / );
        if(separatorIndex==-1)return false; // Return false if no / found
        var expDateArr=expDate.split( / ); 
        if(expDateArr.length>2)return false; // Return false if no num/num format found
        // Check Month for validity
        if(eval(expDateArr[0])<1||eval(expDateArr[0])>12)
        {
            //If month is not valid i.e not in range 1-12
            return false;
        }
        //Check Year for format YY or YYYY
        switch(expDateArr[1].length)
        {
            case 2:expYearCheck=2000+parseInt(expDateArr[1], 10);break; // If YY format convert it to 20YY to it
            case 4:expYearCheck=expDateArr[1];break; // If YYYY format assign it to check Year Var
            default:return false;break;
        }
        // Calculated new exp Date for ja  
        expDate=new Date(eval(expYearCheck),(eval(expDateArr[0])-1),1,0,0,0,0);
        if(Date.parse(startDate) <= Date.parse(expDate))
        {
            if(Date.parse(expDate) <= Date.parse(futureLimitDate))
            {
                // Date validated
                return true;    
            }else
            {
                // Date exceeds future date
                return false;
            }
        }else
        {
            // Date is earlier than todays date
            return false;
        }
    },
    "<br />Must be a valid Expiration Date."
    );

我的到期日有两个字段如下:

<select name="qbms_cc_expires_month" id="qbms_cc_expires_month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select name="qbms_cc_expires_year" id="qbms_cc_expires_year">
<option value="12">2012</option>
<option value="13">2013</option>
<option value="14">2014</option>
<option value="15">2015</option>
<option value="16">2016</option>
<option value="17">2017</option>
<option value="18">2018</option>
<option value="19">2019</option>
<option value="20">2020</option>
<option value="21">2021</option>
</select>

我在第二选的结尾添加了这个字段:

<input type="text" name="qbmsCombined" id="qbmsCombined" value="" style="width:45px;height:16px;" readonly="readonly">

和我的j Query 像这样

$().ready(function() {
    // combine exp date fields and validate new field
    $("#qbms_cc_expires_month,#qbms_cc_expires_year").change(function() {
       $("input[type= text ][name= qbmsCombined ]").val($("#qbms_cc_expires_month").val() +  /  + $("#qbms_cc_expires_year").val());

    });
    // validate signup form on keyup and submit
    $("#checkoutForm").validate({
        rules: {
        qbms_cc_owner: {
                required: true
            },
        qbms_cc_number: {
                required: true,
                minlength: 13,
                maxlength: 16,
                creditcard: true
            },
            qbmsCombined: {
                ccexpdate: true
          },
          qbms_cc_cvv2: {
                required: true,
                minlength: 3,
                maxlength: 4
            },      
        },
        messages: {

            qbms_cc_owner: {
                required: "<br />Please enter the name on the Credit Card"
            },
            qbms_cc_number: {
                required: "<br />Please enter the Credit Card Number",
                minlength: "<br />Credit Card Number must be between 13 and 16 digits",
                maxlength: "<br />Credit Card Number is over 16 digits",
                creditcard: "<br />Credit Card Number is invalid"
            },
              qbms_cc_cvv2: {
                required: "<br />Please enter the Credit Card s Security Code",
                minlength: "<br />Credit Card s Security Code must be at least 3 digits",
                maxlength: "<br />Credit Card s Security Code must be 4 digits or less"
            },
        }

    });

    $( form#checkoutForm ).submit(function(evt){
        if($( #checkoutForm ).valid())
            return true
        else
        evt.preventDefault();
            return false;

    });
});




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签