Technologies:
Tolerim
a month ago
My HTML isn't functioning properly with the JavaScript I programmed.
The provided code is for an online bookstore, where the HTML and CSS have already been created. However, the developer is facing issues with the JavaScript code. They need to create code that checks the validity of input data by the user when they click on the “Pay Now” button. The code provided checks whether the inputted value matches the pattern of 16-digits starting with 51-55. If it does, the customer is redirected to the success page; otherwise, they are shown an alert message stating that they must enter a valid card number. The HTML code of the page that links to this JavaScript file is also included, which includes input fields for name, card number, expiry date, and CVV, as well as a “Pay Now” button. The developer seeks guidance on this specific issue.
Answers(2)
Vogi
a month ago
Verified Answer
I can help you with that. Here is an example of JavaScript code that validates credit card input:
function validateCardNumber(cardNumber) {
var regex = new RegExp(\"^(?:5[1-5][0-9]{14})$\");
if (!regex.test(cardNumber))
return false;
return true;
}
This code checks whether the inputted value matches the pattern of 16-digits starting with 51-55. If it does, the customer is redirected to the success page; otherwise, they are shown an alert message stating that they must enter a valid card number.
Here is an example of how you can use this function:
if (validateCardNumber(document.getElementById(\"cardNumber\").value)) {
window.location.href = \"success.html\";
} else {
alert(\"Please enter a valid card number.\");
}
I hope this helps! Let me know if you have any other questions.
Sources
javascript - How to validate a credit card number - Stack Overflowcredit card input validation using regular expression in javascriptJavaScript : Credit Card Number validation - w3resourceTolerim
a month ago
The JavaScript code provided seems to be working correctly. It listens for the click on the "Pay Now" button, checks the validity of the entered card number using a regular expression, and then redirects to the success page if the card number is valid, or shows an alert box with an error message if it is not. However, there are a few issues with the HTML markup that could be improved:
- The element should be inside the of the HTML document, not in the element.
- The element should come before the element in the document order for better SEO and accessibility.
- The to the stylesheet should come after the section, inside the section.
- The for the expiry year select element is missing.
Other than that, the HTML markup looks good and should work properly with the provided JavaScript code.