Catalog Variable field validation

Nishi Khangarka
Kilo Contributor

Hi All,

 

We have a single line text field on the catalog form where below 4 validations are required.

1. 0-9 digits only
2. space or ',' should be consider
3. pattern 0000-9999

4. Only 10 Numbers should be expected.

 

sample : 

1000 , 1001 , 1002 , 1003 , 1004 , 1005 , 1006 , 1007 , 1008 , 1009

1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009

1000,1001,1002,1003,1004,1005,1006,1007,1008,1009

 

Kindly help us with the  script on the same.

Thanks

1 REPLY 1

Marco0o1
Tera Sage

Hi @Nishi Khangarka :

This is how i understand, this is a function to validate if is valid, you just will add in your script as function and call when you need:

 

 

function validateInput(input) {
  // Remove spaces from the input
  var sanitizedInput = input.replace(/\s/g, '');

  // Separate by ","
  var numbersArray = sanitizedInput.split(',');

  // Check if there are exactly 10 numbers
  if (numbersArray.length !== 10) {
    return false;
  }

  // Check if each number matches the pattern 0000-9999
  for (var i = 0; i < numbersArray.length; i++) {
    var number = numbersArray[i];
    if (!/^\d{4}$/.test(number) || number < 0 || number > 9999) {
      return false;
    }
  }

  return true;
}

// Example usage:
var input1 = "1000 , 1001 , 1002 , 1003 , 1004 , 1005 , 1006 , 1007 , 1008 , 1009";
gs.print(validateInput(input1)); // true

var input2 = "1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009";
gs.print(validateInput(input2)); // true

var input3 = "1000,1001,1002,1003,1004,1005,1006,1007,1008,1009";
gs.print(validateInput(input3)); // true

// Examples returning false:

var input4 = "12 34567890"; // Not exactly 10 digits
gs.print(validateInput(input4)); // false

var input5 = "abcd, 1001, 1002, 1003, 1004"; // Contains non-digit characters
gs.print(validateInput(input5)); // false

var input6 = "9999, 10000, 1001, 1002, 1003"; // Number 10000 is not in the range 0000-9999
gs.print(validateInput(input6)); // false

var input7 = "1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008"; // Only 9 numbers, expecting 10
gs.print(validateInput(input7)); // false

 

This is what i understand from your instruction, you can build a client script onChange you field and the throw error or alert if the field doesnt meet your parameters. Hope that help you.