简体   繁体   English

为什么我的信用卡验证无效?

[英]Why isn't my credit card validation working?

I'm adding a credit card validation to a form. 我正在向表单添加信用卡验证。

If the user clicks the register button and there is a not a valid credit card number then there will be an error flagged, the label will turn red. 如果用户单击注册按钮,但没有有效的信用卡号,则将标记错误,标签将变为红色。

It also makes sure that CVV field and zip code field are not empty. 它还确保CVV字段和邮政编码字段不为空。

Right now if I click the button when the fields are empty, I get the errors as needed. 现在,如果我在字段为空的情况下单击按钮,则会收到所需的错误。

If I then fill the zip code and CSV and click register again, then the error are removed. 如果然后我填写邮政编码和CSV,然后再次单击注册,则错误将被消除。

Nothing removes the CC number flag though. 但是,什么也不会删除CC号标志。

Why is this and how can I fix it? 为什么会这样,我该如何解决?

Code is below, thank you: 代码如下,谢谢:

// Form validation. Display error messages and don't let the user submit the form if any of these validation errors exist:
document.querySelector("button").addEventListener("click", function(e) {

    //check there's a valid credit card number
    var ccNum = document.getElementById("cc-num");
    var ccNumLbl = document.getElementById("cc-numLbl");


// takes the form field value and returns true on valid number
//algorithm found here: https://gist.github.com/DiegoSalazar/4075533
function valid_credit_card(value) {
  // accept only digits, dashes or spaces
    if (/[^0-9-\s]+/.test(value)) return false;

    // The Luhn Algorithm. It's so pretty.
    var nCheck = 0, nDigit = 0, bEven = false;
    value = value.replace(/\D/g, "");

    for (var n = value.length - 1; n >= 0; n--) {
        var cDigit = value.charAt(n),
              nDigit = parseInt(cDigit, 10);

        if (bEven) {
            if ((nDigit *= 2) > 9) nDigit -= 9;
        }

        nCheck += nDigit;
        bEven = !bEven;
    }

    return (nCheck % 10) == 0;
}


    if(!valid_credit_card(ccNum.input)) {
        ccNumLbl.style.color = "red";
        e.preventDefault();
    } else {
        ccNumLbl.style.color = "black";
    }

    //check there's a zip code
    var zip = document.getElementById("zip");
    var zipLbl = document.getElementById("zipLbl");
    if(zip.value.length === 0) {
        zipLbl.style.color = "red";
        e.preventDefault();

    } else {
        zipLbl.style.color = "black";
    }

    //check there's a cvv
    var cvv = document.getElementById("cvv");
    var cvvLbl = document.getElementById("cvvLbl");
    if(cvv.value.length === 0) {
        cvvLbl.style.color = "red";
        e.preventDefault();

    } else {
        cvvLbl.style.color = "black";
    }

});

And the html: 和html:

<div id="credit-card" class="credit-card">

  <div class="col-6 col">
    <label id="cc-numLbl" for="cc-num">Card Number:</label>
    <input id="cc-num" name="user_cc-num" type="text">
  </div>

  <div class="col-3 col">
    <label for="zip" id="zipLbl">Zip Code:</label>
    <input id="zip" name="user_zip" type="text"> 
  </div>

  <div class="col-3 col">
    <label id="cvvLbl" for="cvv">CVV:</label>
    <input id="cvv" name="user_cvv" type="text"> 
  </div>

  <label>Expiration Date:</label>
  <select id="exp-month" name="user_exp-month">
    <option value="1">1 - January</option>
    <option value="2">2 - February</option>
    <option value="3">3 - March</option>
    <option value="4">4 - April</option>
    <option value="5">5 - May</option>
    <option value="6">6 - June</option>
    <option value="7">7 - July</option>
    <option value="8">8 - August</option>
    <option value="9">9 - September</option>
    <option value="10">10 - October</option>
    <option value="11">11 - November</option> 
    <option value="12">12 - December</option>                       
  </select>  
  <select id="exp-year" name="user_exp-year">
    <option value="2016">2016</option>
    <option value="2017">2017</option>
    <option value="2018">2018</option>
    <option value="2019">2019</option>
    <option value="2020">2020</option>                      
  </select>                                
</div>

<button type="submit">Register</button> 

Seems you are trying to get the text input object instead of its value. 似乎您正在尝试获取文本输入对象而不是其值。 You need to change your ccNum variable assignment to, 您需要将ccNum变量分配更改为

var ccNum = document.getElementById('cc-num').value;

Also the querySelector function was failing in your code for which I have made the <input type="button"...> declaration as (added a class attribute to it), 同样, querySelector函数在您的代码中失败了,为此我将<input type="button"...>声明为(向其添加了类属性),

<input type="button" class="button" id="myBtn" value="Validate" />

And changed the document.querySelector(...) function as 并将document.querySelector(...)函数更改为

document.querySelector(".button").addEventListener("click", function(e) {

Working DEMO : https://jsfiddle.net/nmvk9ks7/1/ 工作演示https : //jsfiddle.net/nmvk9ks7/1/

Hope this helps! 希望这可以帮助!

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

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