0

マスクを付けてクレジットカードフィールドを書いていますが、実際の未変更の番号を要素の data 属性に保存しています。このように、クレジットカード番号「6011000000000004」は使用のために保存されますが、「601100******0004」が画面に表示されます。

Jquery Validator を使用して、データ属性に基づいてそのフィールドを検証する方法はありますか?

私の形で私は持っています

<input type="text" id="creditcard" />

次に、ユーザーが値を変更すると、マスクが作成され、次のように値が $("#creditcard").data("cc_number") に保存されます。

$(document).on("keyup paste drop", "#creditcard", function() {
    // Copy the super secret old cc value */    
    var old_cc = $(this).data("cc_number") || "";

    // Copy the masked value 
    var this_val = $(this).val();

    /* If there is a value and it is less than the max create a mask */ 
    if (this_val.length > 0 && this_val.length <= 16) {
        // If the stored number is less than the new value, 
        // append the last entered character to the stored value.
        // If not make the masked cc # the same as the stored value     
        var cc = (old_cc.length < this_val.length ? old_cc + this_val.substring(this_val.length -1, this_val.length) : old_cc.substring(0,this_val.length));

    // Copy the new cc value to the super secret field
    $(this).data("cc_number", cc);

    /* Code that builds cc_mask
     *  removed to make it easier to read
     */

    // Sets #creditcard value to the mask
    $(this).val(cc_mask);

}

// I don't think we need to do this but since I already have it here...
return false;
});

$("#creditcard".data("cc_number"); の値を検証するために jquery バリデーターをオーバーライドする方法はありますか?

これを行うためのより良い方法に関する提案はありますか?

4

1 に答える 1