Form Validation for jscript. This is the requirement. I just need the code and attached to my jscode. Payment Information Validation Validate the customer’s payment information as follows: Expiry Date (Month) Required field Expiry Date Valid expiry date to ensure the card being used has not expired Card Number Required field, Valid Card Number NOTE: regarding the expiry date, you MUST use the date object to ensure that your date check is dynamic. here is the given html code. Expiry Date * You must choose a month * Invalid expiry date
Form Validation for jscript.
This is the requirement. I just need the code and attached to my jscode.
Payment Information Validation
Validate the customer’s payment information as follows:
Expiry Date (Month) |
Required field |
Expiry Date |
Valid expiry date to ensure the card being used has not expired |
Card Number |
Required field, Valid Card Number |
NOTE: regarding the expiry date, you MUST use the date object to ensure that your date check is dynamic.
here is the given html code.
<li>
<label for="month">Expiry Date</label>
<select id="month" name="month">
<option>- Month -</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select id="year" name="year">
</select>
<p class="cardError error" id="month_error">* You must choose a month</p>
<p class="cardError error" id="expiry_error">* Invalid expiry date</p>
</li>
here is my existing jscode.
function validate(e) {
// Hides all error elements on the page
hideErrors();
// Determine if the form has errors
if (formHasErrors()) {
// Prevents the form from submitting
e.preventDefault();
// When using onSubmit="validate()" in markup, returning false would prevent
// the form from submitting
return false;
}
// When using onSubmit="validate()" in markup, returning true would allow
// the form to submit
return true;
}
/*
* Handles the reset event for the form.
*
* param e A reference to the event object
* return True allows the reset to happen; False prevents
* the browser from resetting the form.
*/
function resetForm(e) {
// Confirm that the user wants to reset the form.
if (confirm('Clear order?')) {
// Ensure all error fields are hidden
hideErrors();
// Set focus to the first text field on the page
document.getElementById("qty1").focus();
// When using onReset="resetForm()" in markup, returning true will allow
// the form to reset
return true;
}
// Prevents the form from resetting
e.preventDefault();
// When using onReset="resetForm()" in markup, returning false would prevent
// the form from resetting
return false;
}
/*
* Does all the error checking for the form.
*
* return True if an error was found; False if no errors were found
*/
function formHasErrors() {
// Determine if any items are in the cart
// When the cart has not items, submission of form is halted
if (numberOfItemsInCart == 0) {
// Display an error message contained in a modal dialog element
const modal = document.querySelector("#cartError");
modal.showModal();
const closeModal = document.querySelector(".close-button");
closeModal.addEventListener("click", () => {
modal.close();
document.getElementById("qty1").focus();
});
// Form has errors
return true;
}
// Complete the validations below
let errorFlag = false;
let requiredFields = ["fullname", "address", "city", "province", "postal","email", "cardname", "month", "year", "cardnumber"];
for(let i=0; i<requiredFields.length; i++){
let textField = document.getElementById(requiredFields[i]);
if(!leftHasInput(textField)){
document.getElementById(requiredFields[i] + "_error").style.display ="block";
if(!errorFlag){
textField.focus();
textField.select();
}
//Raise the error flag
errorFlag = true;
}
}
return errorFlag;
}
Step by step
Solved in 3 steps with 1 images