简体   繁体   English

javascript 表单实时输入验证,oninput 无法按预期工作

[英]javascript form realtime input validation with oninput not working as expected

I have a list of valid promo codes, say ["one","two","three"] I have an input field called promo code where users can input their promo code, if they have one.我有一个有效促销代码的列表,比如["one","two","three"]我有一个名为promo code的输入字段,用户可以在其中输入他们的促销代码,如果他们有的话。

When the user's input matches an item in the list, we should do X. If the user's input does not match an item in the list, we should do Y.当用户的输入列表中的一个项目匹配时,我们应该做X。如果用户的输入与列表中的一个项目匹配,我们应该做Y。

code that works:有效的代码:

JS: JS:

    function checkPromo() {
        var promoCodes = [{% for code in promo_codes %} "{{code}}", {% endfor %}];
        var arrayLength = promoCodes.length;
        var enteredCode = document.getElementById('promoCode').value;
        var message = document.getElementById('formErrorMessage');

        for (var i = 0; i < arrayLength; i++) {
            if (enteredCode == promoCodes[i]) {
                message.innerHTML = '<br /><p class="promoMessageValid">Your promo code is valid!</p>';
            } 
        }                                                             
    }

HTML: HTML:

<form>
    ...                                                             
    <div class="row">
        <div class="field half-width">
            <input type="text" placeholder="Promo Code" class="input empty" id="promoCode" maxlength="8" oninput="checkPromo()">
            <label for="promo">Promo Code</label>
            <div class="baseline"></div>
        </div>
    </div>                                                         
    ...                                                            
    <div class="error field" role="alert">
        <span class="message" id="formErrorMessage"></span>
    </div>                                          
</form>

Code that doesn't work:不起作用的代码:

JS: JS:

function checkPromo() {
    var promoCodes = [{% for code in promo_codes %} "{{code}}", {% endfor %}];
    var arrayLength = promoCodes.length;
    var enteredCode = document.getElementById('promoCode').value;
    var message = document.getElementById('formErrorMessage');

    for (var i = 0; i < arrayLength; i++) {
        if (enteredCode == promoCodes[i]) {
            message.innerHTML = '<br /><p class="promoMessageValid">Your promo code is valid!</p>';
        } else if (enteredCode == '') {
            message.innerHTML = '';
        } else {
            message.innerHTML = '<br /><p class="promoMessageInvalid">Your promo code is invalid';
        }
  }
}

The HTML is the same in both cases.两种情况下的 HTML 都是相同的。

The code that doesn't work isn't completely broken;不起作用的代码并没有完全损坏; It displays "your promo code is invalid" correctly.它正确显示“您的促销代码无效”。 It also changes the 'formErrorMessage' to display an empty string when the promo code input is empty.当促销代码输入为空时,它还会更改“formErrorMessage”以显示空字符串。 Yet it doesn't register when the entered promo code matches one of the valid promo codes..然而,当输入的促销代码与有效促销代码之一匹配时,它不会注册..

What am I doing wrong?我究竟做错了什么?

Thanks in advance!提前致谢!

In the case you found a valid promoCode matching the input, you should break , otherwise your "valid" result may be overridden.如果您发现与输入匹配的有效promoCode ,您应该break ,否则您的“有效”结果可能会被覆盖。 You code will display "Your promo code is valid!"您的代码将显示“您的促销代码有效!” only if the input code is the last in the promoCodes array.当输入代码是promoCodes数组中的最后一个时。

Another way of doing this, without looping explicitly (thus avoiding this kind of override):这样做的另一种方法,无需显式循环(从而避免这种覆盖):

if (!enteredCode) {
  message.innerHTML = '';
} else if (promoCodes.includes(enteredCode)) {
  message.innerHTML = '<br /><p class="promoMessageValid">Your promo code is valid!</p>';
} else {
  message.innerHTML = '<br /><p class="promoMessageInvalid">Your promo code is invalid';
}

Alternatively, promoCodes.indexOf(enteredCode) >= 0 if you don't like includes .或者, promoCodes.indexOf(enteredCode) >= 0如果您不喜欢includes Like for MSIE compatibility I suppose.就像我想的 MSIE 兼容性。

That being said:话虽如此:

users can input their promo code, if they have one用户可以输入他们的促销代码,如果他们有的话

If these "promo codes" are not supposed to be known by everybody, you may want to keep them on the server and check the validity from there, and not send them to the client: one would just need to display the source to find coupon codes for free.如果不应该每个人都知道这些“促销代码”,您可能希望将它们保留在服务器上并从那里检查有效性,而不是将它们发送给客户端:只需要显示来源即可找到优惠券代码免费。

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

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