2009-08-25 9 views
2

J'ai écrit du code qui va vérifier deux dates - elles sont divisées en entrées de deux jours (# enddate-1-dd, # date-1-dd), entrées de deux mois (# enddate-1-mm, #date -1 mm) et deux entrées d'entrée (# enddate-1, # date-1)Puis-je simplifier/optimiser ce code Jquery?

Je voulais d'abord vérifier qu'il s'agit bien de chiffres, mais je voulais vérifier chacun d'entre eux pour m'assurer il est dans un format de date, au moment où il est comme ça:

function validate_form() { 

retVal = true; // if the statements below fail, return true 

if(retVal == true) { 
    // check whether the available hours they've entered are a valid time! 
    $(":text").each(function() { 
     $this = $(this); // cache the object 
     if (isNaN($this.val())) { 
      $this.focus(); 
      $.jGrowl('Please enter a valid date!', { theme: 'smoke' }); 
      retVal = false; return false; 
     } 
    }); 
} 

if(retVal == true) { 
    $("#date-1-dd").each(function() { 
     $this = $(this); // cache the object 
     if ($this.val() > 31) { 
      $this.focus(); 
      $.jGrowl('Please enter a valid day, should be no more than 31!', { theme: 'smoke' }); 
      retVal = false; return false; 
     } 
    }); 
} 

if(retVal == true) { 
    $("#enddate-1-dd").each(function() { 
     $this = $(this); // cache the object 
     if ($this.val() > 31) { 
      $this.focus(); 
      $.jGrowl('Please enter a valid day, should be no more than 31!', { theme: 'smoke' }); 
      retVal = false; return false; 
     } 
    }); 
} 

if(retVal == true) { 
    $("#date-1-mm").each(function() { 
     $this = $(this); // cache the object 
     if ($this.val() > 12) { 
      $this.focus(); 
      $.jGrowl('Please enter a valid month, should be no more than 12!', { theme: 'smoke' }); 
      retVal = false; return false; 
     } 
    }); 
} 

if(retVal == true) { 
    $("#enddate-1-mm").each(function() { 
     $this = $(this); // cache the object 
     if ($this.val() > 12) { 
      $this.focus(); 
      $.jGrowl('Please enter a valid month, should be no more than 12!', { theme: 'smoke' }); 
      retVal = false; return false; 
     } 
    }); 
} 

if(retVal == true) { 
    $("#date-1").each(function() { 
     $this = $(this); // cache the object 
     if ($this.val() < 1900 || $this.val() > 3000) { 
      $this.focus(); 
      $.jGrowl('Please enter a valid year!', { theme: 'smoke' }); 
      retVal = false; return false; 
     } 
    }); 
} 

if(retVal == true) { 
    $("#enddate-1").each(function() { 
     $this = $(this); // cache the object 
     if ($this.val() < 1900 || $this.val() > 3000) { 
      $this.focus(); 
      $.jGrowl('Please enter a valid year!', { theme: 'smoke' }); 
      retVal = false; return false; 
     } 
    }); 
} 

return retVal; // return either true or false, depending on what happened up there!^

}

Désolé s'il semble que je pose une question idiote, que mon code fonctionne bien, je pense que c'est un manière de déchets le faire, avec beaucoup de répétitions, mais je ne peux pas vraiment penser à un moyen de le faire plus efficacement?

Merci

Répondre

2
function validate_form_checks() { 
    var error; 
    // check whether the available hours they've entered are a valid time! 
    $(':text').each(function() { 
     if(isNaN($(this).val())) { 
      $(this).focus(); 
      error = 'Please enter a valid date!'; 
      return false; 
     } 
    }); 
    if(error) 
     return error; 
    $('#date-1-dd, #enddate-1-dd').each(function() { 
     if($(this).val() > 31) { 
      $(this).focus(); 
      error = 'Please enter a valid day, should be no more than 31!'; 
      return false; 
     } 
    }); 
    if(error) 
     return error; 
    $('#date-1-mm, #enddate-1-mm').each(function() { 
     if($(this).val() > 12) { 
      $(this).focus(); 
      error = 'Please enter a valid month, should be no more than 12!'; 
      return false; 
     } 
    }); 
    if(error) 
     return error; 
    $('#date-1, #enddate-1').each(function() { 
     if($(this).val() < 1900 || $(this).val() > 3000) { 
      $(this).focus(); 
      error = 'Please enter a valid year!'; 
      return false; 
     } 
    }); 
    if(error) 
     return error; 
    return true; 
} 

function validate_form() { 
    var result = validate_form_checks(); 
    if(result === true) { 
     return true; 
    } else { 
     $.jGrowl(result, { theme: 'smoke' }); 
     return false; 
    } 
} 

Bien sûr, la validation qui fournit des informations sur tous les les erreurs sous une forme au lieu de simplement le premier est un peu, tu sais, mieux.

+0

C'est fantastique, merci !! – Nick

1

À première vue, vous pouvez créer une fonction de ces sections identiques et il suffit d'appeler au besoin. You shouldn't repeat yourself.

Voici un blog post sur les dates de validation qui peut être éclairant. Et voici une réponse SO qui donne une validation de jQuery plugin answer à date.

+0

Que signifie donc dans ce contexte? Je l'ai vu avant aujourd'hui! – Dorjan

+0

StackOverflow. Le site sur lequel vous êtes. – chaos

+1

Quelques ressources utiles ici, merci – Nick

0

Oui, ici:

function validate_form() { 
return retVal = !0, retVal == 1 && $(":text") 
    .each(function() { 
    return $this = $(this), isNaN($this.val()) ? ($this.focus(), $.jGrowl("Please enter a valid date!", { 
     theme: "smoke" 
    }), retVal = !1, !1) : void 0 
}), retVal == 1 && $("#date-1-dd") 
    .each(function() { 
    return $this = $(this), $this.val() > 31 ? ($this.focus(), $.jGrowl("Please enter a valid day, should be no more than 31!", { 
     theme: "smoke" 
    }), retVal = !1, !1) : void 0 
}), retVal == 1 && $("#enddate-1-dd") 
    .each(function() { 
    return $this = $(this), $this.val() > 31 ? ($this.focus(), $.jGrowl("Please enter a valid day, should be no more than 31!", { 
     theme: "smoke" 
    }), retVal = !1, !1) : void 0 
}), retVal == 1 && $("#date-1-mm") 
    .each(function() { 
    return $this = $(this), $this.val() > 12 ? ($this.focus(), $.jGrowl("Please enter a valid month, should be no more than 12!", { 
     theme: "smoke" 
    }), retVal = !1, !1) : void 0 
}), retVal == 1 && $("#enddate-1-mm") 
    .each(function() { 
    return $this = $(this), $this.val() > 12 ? ($this.focus(), $.jGrowl("Please enter a valid month, should be no more than 12!", { 
     theme: "smoke" 
    }), retVal = !1, !1) : void 0 
}), retVal == 1 && $("#date-1") 
    .each(function() { 
    return $this = $(this), 1900 > $this.val() || $this.val() > 3e3 ? ($this.focus(), $.jGrowl("Please enter a valid year!", { 
     theme: "smoke" 
    }), retVal = !1, !1) : void 0 
}), retVal == 1 && $("#enddate-1") 
    .each(function() { 
    return $this = $(this), 1900 > $this.val() || $this.val() > 3e3 ? ($this.focus(), $.jGrowl("Please enter a valid year!", { 
     theme: "smoke" 
    }), retVal = !1, !1) : void 0 
}), retVal 
}