Posted By

MMDeveloper on 12/29/09


Tagged

form validation


Versions (?)

batch check for blank form fields


 / Published in: JavaScript
 

URL: http://skyward.nefec.org

Just a prototype based method to quickly check a form for empty fields.

  1. function checkForEmptyFields (elIdArray) {
  2. var returnVal = true;
  3. var returnMsg = [];
  4.  
  5. elIdArray.each( function(elId) {
  6. var el = $(elId);
  7. if (el.value.blank() === true) {
  8. returnVal = false;
  9. returnMsg.push("The element '" + el.name + "' cannot be blank.");
  10. } else {}
  11. });
  12.  
  13. return [returnVal, returnMsg];
  14. }
  15.  
  16.  
  17. var checkFields = checkForEmptyFields(["firstName", "lastName", "userEmail"]);
  18.  
  19. if (checkFields[0] === false) {
  20. alert(checkFields[1]);
  21. }
  22. else {
  23. $("updateProfile").submit();
  24. }

Report this snippet  

You need to login to post a comment.