Created
March 23, 2012 13:54
-
-
Save dazld/2170865 to your computer and use it in GitHub Desktop.
Basic JS Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!site || typeof(site) !== 'object') { | |
var site = {}; | |
}; | |
site.validate = { | |
init: function(){ | |
// bind all buttons to form validation and submission | |
this.bindSubmits(false); | |
if ($('form[fact]').length == 1) { | |
$('form[fact] input:visible:first').focus(); | |
}; | |
}, | |
settings: { | |
errors: false, | |
elements: '#admin_form' | |
}, | |
emailPattern: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i, | |
urlPattern: /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i, | |
is_numeric: function(mixed_var){ | |
return (typeof(mixed_var) === 'number' || typeof(mixed_var) === 'string') && mixed_var !== '' && !isNaN(mixed_var); | |
}, | |
bindSubmits: function(remove){ | |
var submits = $(".button.submit[vform], .tab.action.update[vform]"); | |
var deletes = $(".form_action.delete"); | |
if (remove) { | |
submits.unbind('click touchend'); | |
deletes.unbind(); | |
}; | |
$.each(submits,function(i){ | |
var targetform = "#"+this.getAttribute('vform'); | |
$(targetform).bind('submit',function(){ | |
return site.validate.check(targetform); | |
}); | |
$(this).bind('click touchend',function(){ | |
$(targetform).submit(); | |
}); | |
}); | |
deletes.live('click touchend',function(e){ | |
var ok = confirm(site.t.are_you_sure); | |
if (ok) { | |
return true; | |
} else { | |
e.preventDefault(); | |
return false; | |
} | |
}); | |
}, | |
check: function(elems) { | |
if ($(elems).length !== 1) { | |
//alert('Something went wrong - please contact an administrator or refresh the page to try again'); | |
return false; | |
}; | |
this.settings.elements = elems; | |
$(this.settings.elements + " :input[valid]").each( | |
function(){ | |
var cval = $(this).val(); | |
/*var ctitle = $(this).attr("title");*/ | |
switch($(this).attr("valid")){ | |
case "required": | |
$(this).toggleClass('error',!cval.length >=1); | |
break; | |
case "numeric": | |
$(this).toggleClass('error',!site.validate.is_numeric(cval)); | |
break; | |
case "email": | |
$(this).toggleClass('error',!site.validate.emailPattern.test(cval)); | |
break; | |
case "url": | |
$(this).toggleClass('error',!site.validate.urlPattern.test(cval)); | |
break; | |
} | |
}) | |
if ($(this.settings.elements + " .error").not("#emsg").length > 0) { | |
this.settings.errors = true; | |
//alert(site.t.errors_found); | |
// is this tab active? show it if not | |
var ptab = $(this.settings.elements).parent('.tab-block').attr('rel'); | |
site.tabcontroller.showtab(ptab); | |
return false; | |
} | |
else { | |
this.settings.errors = false; | |
// var ok = confirm(site.t.are_you_sure); | |
// if (!ok) { | |
// return false; | |
// }; | |
var action = $(this.settings.elements).attr("fact"); | |
$(".tab.action.update").unbind('click'); | |
$(this.settings.elements).attr("action",action); | |
$(this.settings.elements).attr('onsubmit',null); | |
$('body').trigger('before_submit').unbind('before_submit.custom'); | |
$(this.settings.elements).unbind(); | |
$(this.settings.elements).submit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment