Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* function to test the length */
- function testLength(value, length) {
- if(value.length == length) {
- return true;
- }
- return false;
- }
- /* check if the value passed is a real value */
- function testNumber(value) {
- if(isNaN(value)){
- return false;
- }
- return true;
- }
- /* check if the control's value is the correct length */
- function validateControl(control, name, length){
- // the result of this function
- var result = false;
- // test the number
- if(testLength(control.value,length)){
- // test the number
- if(testNumber(control.value)){
- result = true;
- }
- }
- return result;
- }
- /* checks if a credit card is valid or not */
- function validateCreditCard(value){
- value = value.split(" ").join(" ");
- // the result of the function
- var result = true;
- // the test for it being a #
- if(!testNumber(value)){
- console.log("Credit card details entered are not a number");
- return false;
- }
- if(!(Number(value.charAt(0))== 3 || Number(value.charAt(0)) == 4 || Number(value.charAt(0)) == 5 || Number(value.charAt(0)) == 6)){
- Console.log("Invalid Credit card starting with" + Number(value.charAt(0)));
- // if the credit card is invalid
- return false;
- }
- /* if the credit card is valid then check for the length of the digits */
- if(Number(value.charAt(0) == 3)) { // this is for the AmEx
- if(!testLength(value,15)){
- console.log("Invalid Credit Card");
- return false;
- }
- }
- else{
- if(!testLength(value,16)){
- console.log("Invalid Credit Card");
- return false;
- }
- }
- return result;
- }
- /* if the value is greater than today's date or not ) */
- function validateDate(value){
- var todayDate = new Date().getDate();
- if(Number(value.split("-")[2])>todayDate){
- console.log("Wrong Date Selcected");
- return false;
- }
- // the date must be valid
- return true;
- }
- /* test that the email is in the correct format w/ RegEx */
- function validateEmail(value){
- var RegEx = /\S+@\S+\.\S+/;
- return RegEx.test(value);
- }
- /* validate the form */
- function validateForm()
- {
- // we need to get all of the references of dom objects in vars
- var cvc = document.getElementByld("cvc");
- var zip = document.getElementByld("Zip");
- var cc = document.getElementByld("CreditCard");
- var date = document.getElementByld("Date");
- var state = document.querySelector("#State");
- var email = document.getElementByld("Email");
- // check all details entered in the form, is the cvc 3 digits and is the zip 5 digits.
- if (validateControl(cvc, "cvc", 3) && validateControl(zip, "Zip", 5) && validateCreditCard(cc.value) && validateDate(date.value) && validateEmail(email.value) && validateState(state.selectedIndex))
- {
- console.log("Payment Submitted");
- document.querySelector('.output').textContent = "Payment Sent";
- return true;
- }
- document.querySelector('.output').textContent = "Payment Failed";
- return false;
- }
- /* the state selected is checked if its valid */
- function validateState(value){
- var state = document.getElementById("State");
- if(value != state.selectedIndex || state.selectedIndex == 0) {
- console.log("Please select the correct state");
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement