For my homework, one of the things I need to do is create a form to fill in credit card details and have validation check on it. For the sake of simplicity, I'm just going to validate whether the numbers (Credit card number and CVV) are integers and whether they are within a certian range (to check the number of digits that have been input. So a CVV should not be smaller than 100 nor larger than 999).
EDIT: Here's my html code:
<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
When I click the form's submit
button that runs function()
, it simply reloads the page, instead of displaying any alert messages or redirecting to thankyou.php. Any idea how to rewrite my code?