-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.aw-check.min.js
15 lines (11 loc) · 5.04 KB
/
jquery.aw-check.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
Awkward Check - jQuery plugin
http://www.jquery.com
http://www.awkwardgroup.com/sandbox/awkward-check-a-jquery-plugin
http://demo.awkwardgroup.com/check
Version: 1.3.2
Copyright (C) 2011 Awkward Group (http://www.awkwardgroup.com)
Licensed under Attribution-ShareAlike 3.0 Unported
http://creativecommons.org/licenses/by-sa/3.0/
*/
var aw_check_custom_validation=new Array();function aw_check(field){var checkpassed=new Array();jQuery(field+' .input-box').each(function(){var checkfield=jQuery(this).attr('data-checkfield');var checkrule=jQuery(this).attr('data-checkerror');if((checkfield!=''&&checkfield!=undefined)||(checkrule!=''&&checkrule!=undefined)){aw_check_field(checkfield,checkpassed)}});return(jQuery.inArray(false,checkpassed)==-1)?true:false}function aw_check_init(field,autocheck){if(!autocheck){autocheck=true};jQuery(field+' .input-box').each(function(){var checkfield=jQuery(this).attr('data-checkfield');var checklabel=jQuery(this).attr('data-checklabel');if(checkfield!=''&&checkfield!=undefined){if(jQuery(this).hasClass('check')){jQuery(this).find('label').click(function(){if(jQuery(checkfield).is(':checked')){jQuery(checkfield).attr('checked',false)}else{jQuery(checkfield).attr('checked',true)}if(autocheck)aw_check_init_auto_check(this)})}if(checklabel!=''&&checklabel!=undefined){jQuery(checkfield).parents('.input-box').prepend('<span class="label">'+checklabel+'</span>');jQuery(checkfield).parents('.input-box').find('.label').click(function(){checkfield=jQuery(this).parents('.input-box').attr('data-checkfield');jQuery(checkfield).focus()});jQuery(checkfield).keyup(function(){aw_check_toggle_overlay_labels(this)});jQuery(checkfield).change(function(){aw_check_toggle_overlay_labels(this)})}if(autocheck){jQuery(checkfield).change(function(){aw_check_init_auto_check(this)})}}})}function aw_check_toggle_overlay_labels(referer){var checkfield=jQuery(referer).parents('.input-box').attr('data-checkfield');var label=jQuery(referer).parents('.input-box').find('.label');if(jQuery(checkfield).val()!=''){jQuery(label).hide(0)}else{jQuery(label).fadeIn(100)}}function aw_check_init_auto_check(referer){var checkparent=jQuery(referer).parents('.input-box');var checkfield=jQuery(referer).parents('.input-box').attr('data-checkfield');var checkrule=jQuery(referer).parents('.input-box').attr('data-checkrule');var checkrule_array=checkrule.split(':');if(jQuery(checkfield).val()==''&&checkrule_array[1]=='empty'){aw_check_field(referer)}else if(jQuery(checkfield).val()!=''){aw_check_field(referer)}else{aw_check_hide_check(checkparent)}}function aw_check_field(referer,checkpassed){var checkparent=jQuery(referer).parents('.input-box');var checkfield=jQuery(referer).parents('.input-box').attr('data-checkfield');var checkrule=jQuery(referer).parents('.input-box').attr('data-checkrule');var checkerror=jQuery(referer).parents('.input-box').attr('data-checkerror');var value=jQuery(checkfield).val();if(checkpassed==undefined)checkpassed=new Array();var checkrule_array=checkrule.split(':');checkrule=checkrule_array[0];if(checkrule_array[1]=='empty'&&(value=='')){aw_check_hide_check(checkparent);checkpassed.push(true)}else{if(checkrule=='email'){var emailReg=/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;if(value==''||!emailReg.test(value)){aw_check_print_error(checkparent,checkerror);checkpassed.push(false)}else{aw_check_hide_error(checkparent);checkpassed.push(true)}}else if(checkrule=='number'){var numberRegex=/^[\d]+$/;if(!numberRegex.test(value)){aw_check_print_error(checkparent,checkerror);checkpassed.push(false)}else{aw_check_hide_error(checkparent);checkpassed.push(true)}}else if(checkrule=='phone'){var numberRegex=/^[\d\s\+]+$/;if(!numberRegex.test(value)){aw_check_print_error(checkparent,checkerror);checkpassed.push(false)}else{aw_check_hide_error(checkparent);checkpassed.push(true)}}else if(checkrule=='check'){if(!jQuery(checkfield).is(':checked')){aw_check_print_error(checkparent,checkerror);checkpassed.push(false)}else{aw_check_hide_error(checkparent);checkpassed.push(true)}}else if(checkrule=='text'){if(value==''){aw_check_print_error(checkparent,checkerror);checkpassed.push(false)}else{aw_check_hide_error(checkparent);checkpassed.push(true)}}else{var custom_validation=aw_check_custom_validation[checkrule];try{if(!custom_validation.test(value)){aw_check_print_error(checkparent,checkerror);checkpassed.push(false)}else{aw_check_hide_error(checkparent);checkpassed.push(true)}}catch(err){if(typeof custom_validation=='function'){custom_validation(checkparent,value,checkpassed)}else{checkpassed.push(false);alert(err)}}}}}function aw_check_print_error(checkparent,checkerror){if(checkerror!=''||checkerror!=undefined){jQuery(checkparent).find('.error').text(checkerror);jQuery(checkparent).find('.check').attr('title',checkerror)}jQuery(checkparent).removeClass('valid');jQuery(checkparent).addClass('invalid')}function aw_check_hide_error(checkparent){jQuery(checkparent).removeClass('invalid');jQuery(checkparent).addClass('valid')}function aw_check_hide_check(checkparent){jQuery(checkparent).removeClass('invalid');jQuery(checkparent).removeClass('valid')}