Posted By

tomdowning on 09/21/11


Tagged


Versions (?)

Javascript form validate


 / Published in: jQuery
 

  1. function checkFields() {
  2. var ok = true;
  3. var errStr = "Please fill the following fields correctly: \n";
  4. if($('#contactForm #name').val() == 'Name') {
  5. ok = false;
  6. errStr += 'Name \n';
  7. }
  8. if(isValidEmail($('#contactForm #mdrlyj-mdrlyj').val()) == false) {
  9. ok = false;
  10. errStr += 'Email Address \n';
  11. }
  12. if($('#contactForm #cm-f-olrllh').val() == 'Company Name') {
  13. ok = false;
  14. errStr += 'Company Name \n';
  15. }
  16.  
  17. if(!isValidPostcode($('#contactForm #Postcode').val())) {
  18. ok = false;
  19. errStr += 'Postcode \n';
  20. }
  21. if(!ok) {
  22. alert(errStr);
  23. } else {
  24. submitform('#contactForm');
  25. }
  26. }
  27.  
  28. function isValidEmail(email) {
  29. var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  30. var address = email;
  31. if(reg.test(address) == false) {
  32. return false;
  33. } else {
  34. return true;
  35. }
  36. }
  37. function isValidPostcode(p) {
  38. var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
  39. return postcodeRegEx.test(p);
  40. }

Report this snippet  

You need to login to post a comment.