简体   繁体   English

条件语句返回假

[英]Conditional Statement returning false

I'm trying to validate an input field with REGEX. 我正在尝试使用REGEX验证输入字段。 Can anyone help me understand why this conditional statement is returning "Wrong" in the console? 谁能帮助我了解为什么该条件语句在控制台中返回“错误”?

 <script>
            $('form[action*="paypal"]').submit(function(event) {        

                if (/^DAM\d{10}/.test($('input[name="item_name"]'))){
                    console.log("Correct!");
                    return true;
                }
                else {
                    console.log("Wrong!");
                    event.preventDefault();
                }
            });
        </script>

The form im targeting is: 我定位的形式是:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="XN4BQ5WSZZCEQ">
<table>
<tr><td><input type="hidden" value="">Enter your number</td></tr><tr><td>
<input type="text" name="item_name" required></td></tr>
</table>
<input type="image" src="https://www.paypalobjects.com/en_GB/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>

And i'm entering DAM1234567891 into the input field below: 我在下面的输入字段中输入DAM1234567891:

  <input type="text" name="item_name" required></td></tr>

Try this: 尝试这个:

<script
   src="https://code.jquery.com/jquery-3.2.1.js"
   integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
   crossorigin="anonymous"></script>
<form action="paypal.php">
   <input name="item_name" value="DAM1234567891" />
   <input type="submit">
</form>
<script>
   $('form[action*="paypal"]').submit(function(event) {        

       if (/^DAM\d{10}/.test($('input[name="item_name"]').val())){
           console.log("Correct!");
           event.preventDefault();
           return true;
       }
       else {
           console.log("Wrong!");
           event.preventDefault();
       }
   });
</script>

Your input will need to start with DAM and have 10 digits. 您的输入将需要以DAM开头,并且必须有10位数字。

Ahhh, thanks guys. 啊,谢谢大家。 It worked when i put preventDefault in both statements and return true; 当我在两个语句中都加入了preventDefault并返回true时,它就起作用了; above. 以上。

 <script>
                $('form[action*="paypal"]').submit(function(event) {        

                    if (/^DAM\d{10}/.test($('input[name="item_name"]').val())){
                        console.log("Correct!");
                        return true;
                        event.preventDefault();

                    }
                    else {
                        console.log("Wrong!");
                        event.preventDefault();
                    }

                });
        </script>

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

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