2017-06-08 9 views
0

REGEXで入力フィールドを検証しようとしています。この条件文がコンソールで「間違って」戻っている理由を誰にでも理解できますか?偽を返す条件文

<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> 

ターゲットフォームイムは、次のとおりです。

<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> 

と私は下の入力フィールドにDAM1234567891に入っている:

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

どのような価値がありますか? – zero298

+0

私は[mcve]が必要です – j08691

+2

'$( 'input [name =" item_name "]')'は '.val()'を末尾に付けるべきです... –

答えて

0

はこれを試してみてください:

<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> 

あなた入力はDAMとhで開始する必要があります10桁。

+0

ショーン、上記を試してみてください。 –

+0

@Shaun、これをあなたの最善の答えとしますか?私の答えがあなた自身の答えを考え出すのに役立つかもしれないように見えます。 –

0

ああ、ありがとうございます。両方のステートメントにpreventDefaultを入れてtrueを返すとうまくいきました。上記。

<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> 
+0

あなたは 'if-else'文の前に' event.preventDefault() 'を一回だけ必要とします。 'return'ステートメントの後ろにあるので、あなたの' if'ステートメントにあなたが持っているものは今は呼び出されません。 –

+0

こんにちは、面白いです。私はこの方法を試みる前にそれを働かせることができないと思っていました。私はすぐに別のものを持って行き、あなたに結果を返します。ありがとうございました – Shaun