File: /home/imensosw/www/imenso.co/dev/wipit/js/validation.js
$(function(){
var rule = /^(.+)\[(.+)\]$/,
numeric = /^[0-9]+$/,
integer = /^\-?[0-9]+$/,
decimal = /^\-?[0-9]*\.?[0-9]+$/,
email = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i,
alpha = /^[a-z ]+$/i,
alphaNumeric = /^[a-z0-9]+$/i,
alphaDash = /^[a-z0-9_-]+$/i,
natural= /^[0-9]+$/i,
naturalNoZero= /^[1-9][0-9]*$/i,
ip = /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/i,
base64= /[^a-zA-Z0-9\/\+=]/i,
errorFlag=false;
$.fn.displayError = function(msg){
$(this).addClass("invalid");
$(this).next("div.error-flag").show(0,function(){
errorFlag=true;
$(this).html(msg);
});
}
$.fn.hideError=function(){
$(this).removeClass("invalid");
$(this).next("div.error-flag").hide(0,function(){
errorFlag=false;
$(this).empty();
});
}
function isRequired(){
if($(this).is("input[type=checkbox]") && $(this).attr('checked')!=true)
{
$(this).displayError("Required");
return false;
}
var value=$(this).val();
if(value === "" || value === null){
$(this).displayError("Required");
return false;
}
else{
$(this).hideError();
}
return true;
}
function isInteger(){
if(integer.test($(this).val())){
$(this).hideError();
}
else{
$(this).displayError("Integer");
return false;
}
return true;
}
function isDecimal(){
if(decimal.test($(this).val())){
$(this).hideError();
}
else
{
$(this).displayError("Decimal");
return false;
}
return true;
}
function isNumeric(){
if(numeric.test($(this).val())){
$(this).hideError();
}
else{
$(this).displayError("Numeric");
return false;
}
return true;
}
function isEmail(){
if(email.test($(this).val())){
$(this).hideError();
}
else{
$(this).displayError("Invalid Email");
return false;
}
return true;
}
function isAlpha(){
if(alpha.test($(this).val())){
$(this).hideError();
}
else{
$(this).displayError("Alpha Only");
return false;
}
return true;
}
function isAlphaNumeric(){
if(alphaNumeric.test($(this).val())){
$(this).hideError();
}
else{
$(this).displayError("AlphaNumric Only");
return false;
}
return true;
}
function isAlphaDash(){
if(alphaDash.test($(this).val())){
$(this).hideError();
}
else{
$(this).displayError("AlphaDash Only");
return false;
}
return true;
}
function isNatural(){
if(natural.test($(this).val())){
$(this).hideError();
}
else{
$(this).displayError("Natural Only");
return false;
}
return true;
}
function isNaturalNoZero(){
if(naturalNoZero.test($(this).val())){
$(this).hideError();
}
else{
$(this).displayError("Not Zero Only");
return false;
}
return true;
}
function isConfirm(){
if($(this).val()!= "" && $(this).val() === $(':input.password').val()){
$(this).hideError();
}
else{
$(this).displayError("Password does not matched");
return false;
}
return true;
}
function isMaxlength(){
if($(this).val().length > $(this).attr('maxlength')){
return false;
}
}
$('[maxlength]').live('keyup',isMaxlength);
$(':input.required').live('blur',isRequired);
$(':input.email').blur(isEmail);
$(':input.numeric').live('keydown',function(e){
if((e.which <= 57) && (e.which >= 48 ) || (e.which >=96 && e.which <= 105) || e.which==9 || e.which == 8 ){
$(this).hideError();
}
else{
$(this).displayError("Numeric");
return false;
}
});
$(':input.interger').live('blur',isInteger);
$(':input.alpha').live('blur',isAlpha);
$(':input.alphaNumeric').live('blur',isAlphaNumeric);
$(':input.alphaDash').live('blur',isAlphaDash);
$(':input.natural').live('blur',isNatural);
$(':input.naturalNoZero').live('blur',isNaturalNoZero);
$(':input.confirm').live('keyup',isConfirm);
$(':input.required.invalid').live('keyup',isRequired);
$(':input.confirm.invalid').live('keyup',isConfirm);
$('select.required.invalid').live('change',isRequired);
$('input[type=checkbox].required.invalid').live('click',isRequired);
$('.email.invalid').live('keyup',isEmail);
//$('.numeric.invalid').live('keyup',isNumeric);
$('.interger.invalid').live('keyup',isInteger);
$('.alpha.invalid').live('keyup',isAlpha);
$('.alphaNumeric.invalid').live('keyup',isAlphaNumeric);
$('.alphaDash.invalid').live('keyup',isAlphaDash);
$('.natural.invalid').live('keyup',isNatural);
$('.naturalNoZero.invalid').live('keyup',isNaturalNoZero);
$('.validateForm').live('submit',function(event){
$(this).validateForm(event);
});
$.fn.validateForm=function(event){
$(this).find(':input:visible').each(function(){
if($(this).hasClass('required'))
{
if(!isRequired.call(this))
event.preventDefault();
}
if($(this).hasClass('email'))
{
if(!isEmail.call(this))
event.preventDefault();
}
if($(this).hasClass('alpha'))
{
if(!isAlpha.call(this))
event.preventDefault();
}
if($(this).hasClass('integer'))
{
if(!isInteger.call(this))
event.preventDefault();
}
if($(this).hasClass('decimal'))
{
if(!isDecimal.call(this))
event.preventDefault();
}
if($(this).hasClass('confirm'))
{
if(!isConfirm.call(this))
event.preventDefault();
}
if($(this).hasClass('numeric'))
{
if(!isNumeric.call(this))
event.preventDefault();
}
if($(this).hasClass('alphaNumeric'))
{
if(!isAlphaNumeric.call(this))
event.preventDefault();
}
if($(this).hasClass('alphaDash'))
{
if(!isAlphaDash.call(this))
event.preventDefault();
}
if($(this).hasClass('natural'))
{
if(!isNatural.call(this))
event.preventDefault();
}if($(this).hasClass('NaturalNoZero'))
{
if(!isNaturalNoZero.call(this))
event.preventDefault();
}
});
}
});
;