/* * jquery.iforms.js - iForm Script Library based using the jQuery framework * http://heo-iforms.blogger.com/ * * This file contains scripts that deal with the FUNCTIONALITY of an iForm * If you are looking for scripts that deal with STYLING, please also include jquery.iforms.style.js * If you are looking for scripts that deal with UI ELEMENTS, please also include jquery.iforms-ui.js * * There are also additional scripts for QTIP and Date Functions * * Version: 0.1 * Copyright 2012 Scott Morris - http://heo-iforms.blogger.com/ * * Dual licensed under MIT or GPLv2 licenses * http://en.wikipedia.org/wiki/MIT_License * http://en.wikipedia.org/wiki/GNU_General_Public_License * * Requires: jQuery */ ;(function( $ ){ // DECLARATIONS ################################################################################ var lib_settings = { classes: { hidden: 'iform-hidden', clickable: 'iform-clickable', hangingIndent: 'iform-hanging-indent', noPrint: 'iform-no-print', floatLeft: 'iform-float-left', floatRight: 'iform-float-right' }, triggers: { update: 'update', checkSubmit: 'iform-check-submit' }, queue: 'iform-queue', style: 'iform-style', attr: { defaultValue: 'iform-defaultValue', maxSize: 'iform-maxSize' } }; //============================================================================================== var opt_default = { stopAction: false, afterUpdate: undefined, callback: undefined }; // TEXT INPUT FUNCTIONS ######################################################################## $.fn.clearDefaultText = function(opt) { opt = $.extend({},opt_default,opt); $(this).each(function(i) { $(this).attr(lib_settings.attr.defaultValue, $(this).val()) .focus(function() { if ($(this).val() == $(this).attr(lib_settings.attr.defaultValue)) $(this).val(''); }) .blur(function() { if ($(this).val() == '') $(this).val($(this).attr(lib_settings.attr.defaultValue)); }); }); if ($.isFunction(opt.callback)) opt.callback.apply(this); return this; } //============================================================================================== $.fn.maskInput = function(opt) { // Define option defaults var fn_default = { allowText: true, // change to false when you want just numbers; useful for doses, quantities, etc allowNumbers: true, // not sure where you would want to prevent numbers, but it's here allowEnterKey: false, // prevent the user from prematurely submitting the form blacklist: { noModifier: [106,109,186,187,192,219,220,221,222], // see table at bottom for keycode reference shiftKey: [49,51,52,53,54,55,56,106,109,192,219,220,222], ctrlKey: [], altKey: [], ctrlAltKey: [], chars: '\'\\!#$%^&*_=+|[]{}"~`<>;' // chars to remove when text is pasted }, maxSize: 255, // The most that an HEO field can accept onKeyDown: undefined, // triggers after the key is evaluated onKeyAllowed: undefined, // triggers after a key is allowed in maskInput onKeyNotAllowed: undefined, // triggers after a key is not allowed in maskInput onMaxSize: undefined, // triggers on a change that causes the value's length to be maxSize onNotMaxSize: undefined, // triggers on a change that causes the value's length to go from maxSize to something smaller onPaste: undefined // triggers after the Paste event }; opt = $.extend({},opt_default,fn_default,opt); $(this).each(function(i) { // Keydown Event ----------------------------------------------------------------------- $(this).keydown(function(e) { // Start with the assumption that the key will be allowed and only deny it if the criteria are met. var allow = true, isSpecialKey = (e.which <= 46) ? true : false; // Check for the ENTER key --------------------------------------------------------- if ((!opt.allowEnterKey) && (e.which == 13)) { allow = false; } // Check to see if you've already reached the maxSize ------------------------------ if ((allow) && ($(this).val().length >= opt.maxSize) && (!isSpecialKey)) { allow = false; } // No CTRL or ALT ------------------------------------------------------------------ if ((allow) && (!e.ctrlKey) && (!e.altKey)) { // Check Text First (doesn't matter whether SHIFT or not) if ((!opt.allowText) && (e.which >= 65) && (e.which <= 90)) { allow = false; } // Specific to No Modifier ----------------------------------------------------- if ((allow) && (!e.shiftKey) && (((!opt.allowText) && ($.inArray(e.which,[188,189]) > -1)) || ((!opt.allowNumbers) && (e.which >= 48) && (e.which <= 57)) || ((!opt.allowNumbers) && (e.which >= 96) && (e.which <= 105)) || ($.inArray(e.which, opt.blacklist.noModifier) > -1) )) { allow = false; } // Specific to Shift ----------------------------------------------------------- if ((allow) && (e.shiftKey) && (((!opt.allowText) && ($.inArray(e.which,[188,190,191]) > -1)) || ($.inArray(e.which, opt.blacklist.shiftKey) > -1) )) { allow = false; } } // Check Against Ctrl+Key, Alt+Key, and Ctrl+Alt+Key Blacklists -------------------- if (((allow) && (e.ctrlKey) && (!e.altKey)) && ($.inArray(e.which, opt.blacklist.ctrlKey) > -1)) { allow = false; } if (((allow) && (!e.ctrlKey) && (e.altKey)) && ($.inArray(e.which, opt.blacklist.altKey) > -1)) { allow = false; } if (((allow) && (e.ctrlKey) && (e.altKey)) && ($.inArray(e.which, opt.blacklist.ctrlAltKey) > -1)) { allow = false; } // Finally, if not allowed by a set of criteria, stop it from happening ------------ if (allow) { if (($(this).val().length < opt.maxSize)&&($(this).attr(lib_settings.attr.maxSize)==true)) { $(this).attr(lib_settings.attr.maxSize,false); if ($.isFunction(opt.onNotMaxSize)) opt.onNotMaxSize.apply(this); } if ($.isFunction(opt.onKeyAllowed)) opt.onKeyAllowed.apply(this); } else { e.preventDefault(); if ($(this).val().length >= opt.maxSize) { $(this).attr(lib_settings.attr.maxSize,true); if ($.isFunction(opt.onMaxSize)) opt.onMaxSize.apply(this); } else { if ($.isFunction(opt.onKeyNotAllowed)) opt.onKeyNotAllowed.apply(this); } } if ($.isFunction(opt.onKeyDown)) { opt.onKeyDown.apply(this); } }); // Paste Event ------------------------------------------------------------------------- $(this).on('paste',function(e) { // Since there's not a good way to get the contents of the clipboard, run this function just after the text is pasted and change the value as needed. setTimeout(function() { // If there are carriage returns, replace them with regular spaces if ($this.val().indexOf('\n') > -1) { $this.val($this.val().split('\n').join(' ')); } // If there are any values in the blacklisted character list, remove them for (var i=0;i -1) { $this.val($this.val().split(blacklistChar).join('')); } } // If the resulting value is greater than our maxSize, trim off the end if ($this.val().length >= opts.maxSize) { $this.val($this.val().substring(0,opts.maxSize)); $(this).attr(lib_settings.attr.maxSize,true); if ($.isFunction(opt.onMaxSize)) { opt.onMaxSize.apply($this); } } // If the resulting value is less than our maxSize and it previously was at maxSize.. if (($(this).val().length < opt.maxSize)&&($(this).attr(lib_settings.attr.maxSize)==true)) { $(this).attr(lib_settings.attr.maxSize,false); if ($.isFunction(opt.onNotMaxSize)) opt.onNotMaxSize.apply(this); } if ($.isFunction(opts.onPaste)) { opts.onPaste.apply($this); } }); }); }); if ($.isFunction(opt.callback)) opt.callback.apply(this); return this; } //============================================================================================== $.fn.textboxToTextArea = function(opt) { var fn_default = { cols: 30, rows: 1 }; opt = $.extend({},opt_default,fn_default,opt); $(this).each(function(i) { var $textArea = $("