lundi 3 juillet 2017

Design patterns /Better way to write Javascript UI validations

I am having a json objetc with some fileds say 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 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.

Aucun commentaire:

Enregistrer un commentaire