Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- formVal("#form","#submit");
- function formVal(form,sub) {
- //submit button click
- $(sub).on("click",function(e) {
- e.preventDefault();
- //remove errors
- $(".bad").remove();
- //validate elements
- valIt("#firstName",/^[A-Za-z-.,' ]{1,60}$/,"Enter Valid First Name");
- function valIt(ele,reg,text) {
- //loop through elements and check if they are good
- $(ele).each(function() {
- if(reg.test($(this).val())) {
- //is valid
- $(this).removeClass("worse");
- } else {
- //is not valid, add error box
- alert(text);
- $(this).addClass("worse");
- }
- });
- }
- //if form has no .worse than submit
- if($(form).find(".worse").length === 0) {
- $(form).submit();
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement