【发布时间】:2019-07-30 10:31:46
【问题描述】:
对于我的家庭作业,我需要做的一件事是创建一个表格来填写信用卡详细信息并对其进行验证检查。为简单起见,我将验证数字(信用卡号和 CVV)是否为整数以及它们是否在特定范围内(以检查已输入的位数。所以 CVV 不应该小于 100 不大于 999)。
编辑:这是我的 html 代码:
<form name="payment" action="thankyou.php" onsubmit="validation();" method="POST">
<--inputs here-->
<button type="submit" class="btn">Continue to checkout</button>
</form>
validation(){
var num=document.getElementByID("ccnum"); //num and ccnum are the credit card numbers
var safenum=document.getElementByID("cvv"); //cvv and safenum are the safety numbers
if (!(Number.isInteger(num)) || (num>9999999999999999) || (num<1000000000000000)){
window.alert("Invalid credit card number.")
} //detect whether the credit card number is an integer, larger than 9999999999999999, smaller than 1000000000000000 or not
else if (!(Number.isInteger(safenum)) || (safenum>1000) || (safenum<100)){
window.alert("Invalid credit card safety number.")
} //detect whether the safety number is an integer, larger than 999, smaller than 100 or not
else {
window.open("thankyou.php")
} //the inputs are valid and the user is sent to another web page
当我单击运行function() 的表单的submit 按钮时,它只是重新加载页面,而不是显示任何警报消息或重定向到thankyou.php。知道如何重写我的代码吗?
【问题讨论】:
-
您的验证功能可能会正常运行,但提交表单会重定向您,除非您通过 preventDefault() 阻止这种情况的发生。请参阅此答案中的示例:stackoverflow.com/a/17709172/11724378
标签: javascript