Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Refactored "error" setting

By: a guest on Dec 3rd, 2012  |  syntax: JavaScript  |  size: 1.14 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. $(document).on("click",".dialog button", function(e){
  2. (e).preventDefault();
  3.  
  4. var name    = $("input[name='name']").val();
  5. var email   = $("input[name='email']").val();
  6. var message = $("textarea[name='message']").val();
  7.  
  8. var emailReg = /^[a-zA-Z0-9._+-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$/;
  9.  
  10. var nameIsValid = name.length >= 1,
  11.     emailIsValid = emailReg.test(email),
  12.     messageIsValid = message.length >= 1;
  13.  
  14. error = !(nameIsValid && emailIsValid && messageIsValid);
  15.  
  16. // client-side validation
  17. if(!nameIsValid)
  18.     $("input[name='name']").addClass("error");
  19. else
  20.     $("input[name='name']").removeClass("error");
  21.  
  22. if(!emailIsValid)
  23.     $("input[name='email']").addClass("error");
  24. else
  25.     $("input[name='email']").removeClass("error");
  26.  
  27. if(!messageIsValid)
  28.     $("textarea[name='message']").addClass("error");
  29. else
  30.     $("textarea[name='message']").removeClass("error");
  31.  
  32. if (error == false) {
  33.  
  34.     $.ajax({
  35.         type: "post",
  36.         dataType: "json",
  37.         url: "lib/email.php",
  38.         data: $("#contactform").serialize(),
  39.         success: function(data) { }
  40.     });
  41.  
  42.     };
  43.  
  44. });