Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. formVal("#form","#submit");
  2. function formVal(form,sub) {
  3. //submit button click
  4. $(sub).on("click",function(e) {
  5. e.preventDefault();
  6. //remove errors
  7. $(".bad").remove();
  8. //validate elements
  9. valIt("#firstName",/^[A-Za-z-.,' ]{1,60}$/,"Enter Valid First Name");
  10. function valIt(ele,reg,text) {
  11. //loop through elements and check if they are good
  12. $(ele).each(function() {
  13. if(reg.test($(this).val())) {
  14. //is valid
  15. $(this).removeClass("worse");
  16. } else {
  17. //is not valid, add error box
  18. alert(text);
  19. $(this).addClass("worse");
  20. }
  21. });
  22. }
  23. //if form has no .worse than submit
  24. if($(form).find(".worse").length === 0) {
  25. $(form).submit();
  26. }
  27. });
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement