Validators

Basic data sanitization for the iDonate SDK.

This is a code section that is used for validating user input when using SDK giving forms.

Methods

User data input validators for:

  • Phone Number
  • Email Address
  • Credit Card Number
  • Custom Amount
  • ZIP Code
  • Blank Field
  • All Fields
//Validate phone number. Returns true if valid.
function validatePhoneNumber(numberIn) {
     var phoneRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

     if (numberIn.match(phoneRegex)) {
          return true;
     } else {
          return false;
    }
}

//Validate email address. Returns true if valid.
function validateEmail(email) {
     const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
     return re.test(String(email).toLowerCase());
}

//Validate card number. Returns true if valid.
function validateCreditCardNumber(numberIn) {

     var ccNum = numberIn;
     var visaRegEx = /^(?:4[0-9]{12}(?:[0-9]{3})?)$/;
     var mastercardRegEx = /^(?:5[1-5][0-9]{14})$/;
     var amexpRegEx = /^(?:3[47][0-9]{13})$/;
     var discovRegEx = /^(?:6(?:011|5[0-9][0-9])[0-9]{12})$/;
     var isValid = false;

     if (visaRegEx.test(ccNum)) {
         isValid = true;
     } else if (mastercardRegEx.test(ccNum)) {
         isValid = true;
     } else if (amexpRegEx.test(ccNum)) {
         isValid = true;
     } else if (discovRegEx.test(ccNum)) {
         isValid = true;
     }

     if (isValid) {
          return true;
     } else {
          return false;
    }
}

//Validate payment amount. Returns true if valid.
function validateCustomAmount(valIn) {
     return /^-?\d*\.?\d*$/.test(valIn);
}

//Validate ZIP code. Returns true if valid.
function validateZip(valIn) {
     return /\b\d{5}\b/g.test(valIn);
}

//Check for blank field. Returns true if blank.
function checkIfBlank(valIn) {
     if (valIn === "" && valIn != undefined) {
          //Yes is blank
          return true;
     } else {
          //No not blank
          return false;
    }
}

/*
*    Check if all DOM elements with class name 'required-field' are valid.
*   Relies on jQuery blur events that check for validity in 'setup.js'.
*   Returns true if all fields are valid.
*/
function validateAllFields() {
     var allValid = true;
     var elements = $('.required-field');
     var invalids = $('.invalid-data-input');

     //Check for blanks
     for (var i = 0; i < elements.length; i++) {
          if (elements[i].value == "") {
              allValid = false;
         }
     }

     //Check for invalids
     if (invalids.length > 0) {
         allValid = false;
     }

     return allValid;
}

DOM Events

These code sections wait for the page to load before setting up events to validate individual fields.

window.addEventListener('DOMContentLoaded', function () {

     /*
    * VALIDATOR FOR DONATION BUTTON
    */

     $('.required-field').focusout(function () {
          //Validate all fields.
          if (validateAllFields() == true) {
               //Valid
              $('#the-button').removeClass('donation-button-inactive');
          } else {
               //Invalid
              $('#the-button').addClass('donation-button-inactive');
         }
    });

     $('.required-field').focusin(function () {
          //Validate all fields.
          if (validateAllFields() == true) {
               //Valid
              $('#the-button').removeClass('donation-button-inactive');
          } else {
               //Invalid
              $('#the-button').addClass('donation-button-inactive');
         }
     });

     /*
    *   INDIVIDUAL VALIDATORS
    */
     $('#billingAddress-city').blur(function () {
          if (checkIfBlank($('#billingAddress-city').val()) == true) {
               //Blank value, invalid
              $('#billingAddress-city').addClass('invalid-data-input');
          } else {
               //Valid
              $('#billingAddress-city').removeClass('invalid-data-input');
         }
     });

     $('#billingAddress-country').blur(function () {
          if (checkIfBlank($('#billingAddress-country').val()) == true) {
               //Blank value, invalid
              $('#billingAddress-country').addClass('invalid-data-input');
          } else {
               //Valid
              $('#billingAddress-country').removeClass('invalid-data-input');
         }
     });

     $('#billingAddress-address1').blur(function () {
          if (checkIfBlank($('#billingAddress-address1').val()) == true) {
               //Blank value, invalid
              $('#billingAddress-address1').addClass('invalid-data-input');
          } else {
               //Valid
              $('#billingAddress-address1').removeClass('invalid-data-input');
         }
     });

     $('#billingContact-lastName').blur(function () {
          if (checkIfBlank($('#billingContact-lastName').val()) == true) {
               //Blank value, invalid
              $('#billingContact-lastName').addClass('invalid-data-input');
          } else {
               //Valid
              $('#billingContact-lastName').removeClass('invalid-data-input');
         }
     });

     $('#billingContact-firstName').blur(function () {
          if (checkIfBlank($('#billingContact-firstName').val()) == true) {
               //Blank value, invalid
              $('#billingContact-firstName').addClass('invalid-data-input');
          } else {
               //Valid
              $('#billingContact-firstName').removeClass('invalid-data-input');
         }
     });

     $('#billingAddress-zip').blur(function () {
          if (validateZip($('#billingAddress-zip').val()) == true) {
               //Valid
              $('#billingAddress-zip').removeClass('invalid-data-input');
          } else {
               //Invalid
              console.log('zip code invalid: ', $('#billingAddress-zip').val());
              $('#billingAddress-zip').addClass('invalid-data-input');
         }
     });

     $('#paymentAmount').blur(function () {
          if (validateCustomAmount($('#paymentAmount').val()) === true) {
               //Valid
              $('#paymentAmount').removeClass('invalid-data-input');
          } else {
               //Invalid
              $('#paymentAmount').addClass('invalid-data-input');
         }
     });

     $('#billingContact-primaryPhone').blur(function () {
          if (validatePhoneNumber($('#billingContact-primaryPhone').val()) === true) {
               //Valid
              $('#billingContact-primaryPhone').removeClass('invalid-data-input');
          } else {
               //Invalid
              $('#billingContact-primaryPhone').addClass('invalid-data-input');
         }
     });

     $('#billingContact-email').blur(function () {
          if (validateEmail($('#billingContact-email').val()) === true) {
               //Valid
              $('#billingContact-email').removeClass('invalid-data-input');
          } else {
               //Invalid
              $('#billingContact-email').addClass('invalid-data-input');
         }
     });

     $('#cardData-cardNumber').blur(function () {
          if (validateCreditCardNumber($('#cardData-cardNumber').val()) === true) {
              $('#cardData-cardNumber').removeClass('invalid-data-input');
          } else {
              $('#cardData-cardNumber').addClass('invalid-data-input');
         }
    });
});