我正在尝试使用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>
答案 0 :(得分: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开头,并且有10位数字。
答案 1 :(得分:0)
<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>