Guest User

Untitled

a guest
Apr 17th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * !AntiShout.js
  3.  * =============================================================================
  4.  *
  5.  * Dissalow to send text form written with CAPS LOCK on.
  6.  * STOP SHOUT TO ME!!!
  7.  *
  8.  * @date: 04.2014
  9.  * @author: Luke Samorodny
  10.  * @contact: [email protected]
  11.  * @licence: GPL v.3 or higher
  12.  *
  13.  * =============================================================================
  14.  *
  15.  * @usage:
  16.  *
  17.  * - Simple:
  18.  * $(formSelector).antishout()
  19.  *
  20.  * - With options:
  21.  *
  22.  * var options = {
  23.  *      // Messages
  24.  *  capsLockOnAlert     : 'Your caps lock is on! Please disable it!',
  25.  *      toMuchUpperCased    : 'Too much uppercased letters!',
  26.  *      
  27.  *      // How much percent of uppercase can be in the string
  28.  *      upperCaseRatio      : 30,
  29.  *      
  30.  *      // Form elements listen
  31.  *      listen              : {
  32.  *                              textInput   : true,
  33.  *                              textarea    : true
  34.  *                            },
  35.  *                            
  36.  *      // Set minimum characters
  37.  *      minChars            : {
  38.  *                              textInput   : 6,
  39.  *                              textarea    : 100
  40.  *                            }
  41.  * }
  42.  *
  43.  * $(formSelector).antishout(options)
  44.  *
  45.  */
  46.  
  47. ;
  48. (function (defaults, $, window, document, undefined) {
  49.  
  50.     'use strict';
  51.  
  52.     $.extend({
  53.  
  54.         pluginSetup: function (options) {
  55.             return $.extend(defaults, options);
  56.         }
  57.  
  58.     }).fn.extend({
  59.  
  60.             /**
  61.              * Antishout public function
  62.              * =====================================================================
  63.              *
  64.              * @usage: $(selector).antishout({property:'value'});
  65.              */
  66.             antishout: function (options) {
  67.  
  68.                 options = $.extend({}, defaults, options);
  69.  
  70.                 return $(this).each(function () {
  71.  
  72.                     /**
  73.                      * Define some elements
  74.                      * =============================================================
  75.                      *
  76.                      * @type array
  77.                      */
  78.                     var $$ = {
  79.                         form: $(this),
  80.                         alertBox: $(this).find('.message'),
  81.                         keyCode: false,
  82.                         shiftKey: false,
  83.                         i: 0,
  84.                         uCLetters: 0,
  85.                         uCLettersRatio: 0,
  86.                         lock: true
  87.                     };
  88.  
  89.                     /**
  90.                      * Check is caps lock on
  91.                      * =============================================================
  92.                      *
  93.                      * @param {event} e
  94.                      * @returns {Boolean|$$.shiftKey}
  95.                      */
  96.                     function capsLock(e) {
  97.                         var s = String.fromCharCode(e.which);
  98.                         if (s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey) {
  99.                             return true;
  100.                         } else {
  101.                             return false;
  102.                         }
  103.  
  104.                     };
  105.  
  106.                     /**
  107.                      * Check is ratio UpperCase to LowerCase allowed
  108.                      * =============================================================
  109.                      *
  110.                      * @param {string} string
  111.                      * @returns {Boolean}
  112.                      */
  113.                     function checkUppercaseRatio(string) {
  114.                         $$.uCLetters = string.replace(/[^A-Z]/g, "").length;
  115.                         $$.uCLettersRatio = 100 * $$.uCLetters / $$.i;
  116.  
  117.                         return ($$.uCLettersRatio < options.upperCaseRatio) ? true : false;
  118.                     };
  119.  
  120.                     /**
  121.                      * Display message
  122.                      * =============================================================
  123.                      *
  124.                      * @param {string} alert
  125.                      */
  126.                     function showAlert(alert) {
  127.                         $$.alertBox.text(alert).show();
  128.                     }
  129.  
  130.                     /**
  131.                      * Hide message
  132.                      * =============================================================
  133.                      */
  134.                     function clearAlert() {
  135.                         $$.alertBox.text('').hide();
  136.                     }
  137.  
  138.                     /**
  139.                      * Unlock submit button
  140.                      * =============================================================
  141.                      */
  142.                     function unlockSubmit() {
  143.                         $$.form.find('[type="submit"]').removeAttr('disabled');
  144.                     }
  145.  
  146.                     /**
  147.                      * Lock submit button back
  148.                      * =============================================================
  149.                      */
  150.                     function lockSubmit() {
  151.                         $$.form.find('[type="submit"]').attr('disabled', 'disabled');
  152.                     }
  153.  
  154.                     /**
  155.                      * Start listen text input
  156.                      * =============================================================
  157.                      */
  158.  
  159.                     function listen(element, minChars) {
  160.  
  161.                         $$.form.find(element).each(function () {
  162.                             var $this = $(this);
  163.  
  164.                             $this.on('keypress', function (e) {
  165.                                 if (capsLock(e)) {
  166.                                     showAlert(options.capsLockOnAlert);
  167.                                     $$.lock = true;
  168.                                 } else {
  169.                                     clearAlert();
  170.                                     $$.lock = false;
  171.                                 }
  172.                             });
  173.  
  174.                             $this.on('keyup', function () {
  175.                                 $$.i = $this.val().length;
  176.  
  177.                                 if ($$.i >= minChars) {
  178.                                     if (!checkUppercaseRatio($this.val())) {
  179.                                         showAlert(options.toMuchUpperCased);
  180.                                         $$.lock = true;
  181.                                     } else {
  182.                                         clearAlert();
  183.                                         $$.lock = false;
  184.                                     }
  185.  
  186.                                     if (!$$.lock) {
  187.                                         unlockSubmit();
  188.                                     } else {
  189.                                         lockSubmit();
  190.                                     }
  191.                                 }
  192.                             });
  193.                         });
  194.                     }
  195.  
  196.                     if (options.listen.textInput) {
  197.                         listen('input[type="text"]', options.minChars.textInput);
  198.                     }
  199.  
  200.                     if (options.listen.textarea) {
  201.                         listen('textarea', options.minChars.textarea);
  202.                     }
  203.  
  204.  
  205.                 });
  206.             }
  207.         });
  208. })({
  209.  
  210.     // Messages
  211.     capsLockOnAlert: 'Your caps lock is on! Please disable it!',
  212.     toMuchUpperCased: 'Too much uppercased letters!',
  213.  
  214.     // How much percent of uppercase can be in the string
  215.     upperCaseRatio: 30,
  216.  
  217.     // Form elements listen
  218.     listen: {
  219.         textInput: true,
  220.         textarea: true
  221.     },
  222.  
  223.     // Set minimum characters
  224.     minChars: {
  225.         textInput: 6,
  226.         textarea: 100
  227.     }
  228.  
  229.  
  230. }, jQuery, window, document);
Advertisement
Add Comment
Please, Sign In to add comment