I am working on an assignment and I well aware of if statements and switch statements. I have this string of code:
if (testLength(document.forms[0].lname) == false) {
isValid = false;
}
if (testLength(document.forms[0].fname) == false) {
isValid = false;
}
if (testLength(document.forms[0].address) == false) {
isValid = false;
}
if (testLength(document.forms[0].summary) == false) {
isValid = false;
}
if (testPattern(document.forms[0].account, /^ACT\d{6}$/) == false) {
isValid = false;
}
if (testPattern(document.forms[0].department, /^DEPT\d{3}$/) == false) {
isValid = false;
}
if (testPattern(document.forms[0].project, /^PROJ\d{5}$/) == false) {
isValid = false;
}
if (testPattern(document.forms[0].ssn, /^\d{3}-\d{2}-\d{4}$|\d{9}$/) == false) {
isValid == false;
}
is there a way to simplify a string of if statements like this if they are all related. I tried to think of different ways you can make a switch, but dont think that was possible. This might be the most elegant solution?
Aucun commentaire:
Enregistrer un commentaire