Client Script Codes - deptster/deptster.github.io GitHub Wiki

Phone Number Validation in Loop

//Creating an Array of Field Values:
var val = [];
val[1] = ZDK.Page.getField('Primary_Mobile');
val[2] = ZDK.Page.getField('Secondary_Mobile');
val[3] = ZDK.Page.getField('Whatsapp_No');

//Defining a Regular Expression for Validation:
const alphanumericRegex = /^[0-9]{10}$/;

var errorCount = 0; //is initialized to 0 to keep track of validation errors.

//Looping Through Fields and Validating:
for (var i = 1; i < val.length; i++) {
  if (val[i] != null) {
// following condition is in case the field is left empty
   if (val[i].getValue() != undefined) {
    if (alphanumericRegex.test(val[i].getValue())) {
    } else {
      errorCount += 1;

    //Displaying Error Messages:
      val[0] = (i == 1) ? val[i].showError('Enter the correct value of Primary Mobile') : "";
      val[0] = (i == 2) ? val[i].showError('Enter the correct value of Secondary Mobile') : "";
      val[0] = (i == 3) ? val[i].showError('Enter the correct value of Whatsapp No') : "";

      log("incorrect value" + i);
    }
   }
  }
  
}

//Returning False if Errors Exist to stop the record from saving:
if (errorCount != 0) {
  return false;
}