Design patterns /Better way to write Javascript UI validations

Viewed 225

I have a json object with some fields: name, promocode, salutation etc.

{
  name: "xx",
  promocode: "asd123",
  salutation: "Mr"
};

For each field I need to check blank as well as conditions to that field.For now I have kept all the field names in an array and I am looping through that array and checking for null/non empty value dynamically. For a specific condition I might have to write some code like:

for (i = 0; i < arr.length; arr++) {
  if (object[arr[i]] == "promocode") {
    validatepromocode(obj.promocode);
  }

  if (object[arr[i]] == "salutation") {
   validatesalutation(obj.promocode)
  }
}

which I feel is not the best way to write such code. Can there be some design pattern/alternative approach to make such code better.

0 Answers
Related