Posted By

onefastsnail on 05/24/12


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

mister
hurcy


simple validation


 / Published in: JavaScript
 

  1. //basic form validation
  2. if($(".js_validate").length>0){
  3. $('.js_validate').submit(function() {
  4.  
  5. //clear other form validations
  6. $('.js_req').removeClass("error");
  7.  
  8. var errors = new Array();
  9. var el = $(this).attr("id");
  10.  
  11. var required = $(this).find(".js_req");
  12.  
  13. required.each(function(i){
  14.  
  15. if($(this).val()==""){
  16. console.log($(this).val());
  17. errors[i]= $(this).attr("name");
  18. $("#"+el+"").find("input[name='"+$(this).attr("name")+"']").addClass("error");
  19. }
  20.  
  21. });
  22.  
  23. if(errors.length>0){
  24. return false;
  25. alert(errors.length);
  26. }
  27. else {
  28. return true;
  29. }
  30.  
  31. });
  32. }

Report this snippet  

You need to login to post a comment.