From 135d0c580b8620c25aa315ea135730d865aa22ec Mon Sep 17 00:00:00 2001 From: aarongustafson Date: Mon, 14 Apr 2014 21:09:01 -0400 Subject: [PATCH] Initial checkin --- README.md | 14 +++- min/jquery.easy-obfuscated-field.js | 2 + src/jquery.easy-obfuscated-field.js | 123 ++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 min/jquery.easy-obfuscated-field.js create mode 100644 src/jquery.easy-obfuscated-field.js diff --git a/README.md b/README.md index f4c3e74..05c95ae 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,12 @@ -jquery.easy-obfuscated-field.js -=============================== +Easy Obfuscated Field +===================== -Obfuscate a field +To use the script, call the `easyObfuscatedField` method +on your jQuery collection: + + $('input').easyObfuscatedField(); + +That will make the field obfuscate on blur and de-obfuscate +on focus. The script creates a hidden field with the original +`name` to maintain the real value so it can be proessed on +the server. \ No newline at end of file diff --git a/min/jquery.easy-obfuscated-field.js b/min/jquery.easy-obfuscated-field.js new file mode 100644 index 0000000..8d7319b --- /dev/null +++ b/min/jquery.easy-obfuscated-field.js @@ -0,0 +1,2 @@ +/* Easy Obfuscated Field (c) Aaron Gustafson (@AaronGustafson). MIT License. https://github.com/easy-designs/jquery.easy-obfuscated-field.js */ +(function(b){var e="easyObfuscatedField",a=e+"-observing",c={visible_suffix:"-visible"};function d(i,h){var g=i.attr("name"),f=i.clone().hide().attr("name",g).removeAttr("id").removeAttr("required");if(f.is("[pattern]")){f.removeAttr("pattern")}i.attr("name",g+h.visible_suffix).after(f)}b.fn.easyObfuscatedField=function(g){g=b.extend(c,g);var j=b(this),f=j.selector,h=b("body"),i=h.data(a)||[];j.each(function(){d(b(this),g)});if(b.inArray(f,i)>-1){return j}i.push(f);h.on("focus",f,function(){var l=b(this),k=l.attr("name"),n,m;if(k.indexOf(g.visible_suffix)<0){d(l)}n=b("input[name="+k.replace(g.visible_suffix,"")+"]");if(n.length&&(m=n.val())!=""){l.val(m)}}).on("blur",f,function(){var l=b(this),m=l.val(),k;b("input[name="+l.attr("name").replace(g.visible_suffix,"")+"]").val(m);if(m.length>3){m=m.split("");k=m.length-3;while(k--){m[k]="*"}l.val(m.join(""))}}).data(a,i);return j}}(jQuery)); \ No newline at end of file diff --git a/src/jquery.easy-obfuscated-field.js b/src/jquery.easy-obfuscated-field.js new file mode 100644 index 0000000..3e67de9 --- /dev/null +++ b/src/jquery.easy-obfuscated-field.js @@ -0,0 +1,123 @@ +/*! Easy Obfuscated Field (c) Aaron Gustafson (@AaronGustafson). MIT License. https://github.com/easy-designs/jquery.easy-obfuscated-field.js */ + +/* Easy Obfuscated Field + * + * To use the script, call the `easyObfuscatedField` method + * on your jQuery collection: + * + * $('input').easyObfuscatedField(); + * + **/ +;(function($){ + + var script_name = 'easyObfuscatedField', + observing_key = script_name + '-observing', + defaults = { + visible_suffix: '-visible' + }; + + function prep_obfuscation( $field, opt ) + { + var name = $field.attr( 'name' ), + $hide = $field + .clone() + // normally I’d set the type to hidden, + // but IE doesn’t like it + .hide() + .attr( 'name', name ) + .removeAttr( 'id' ) + .removeAttr( 'required' ); + + // remove patterns (if set) + // currently running this separately as IE10 is apparently + // throwing an error with it chained on the above + if ( $hide.is( '[pattern]' ) ) + { + $hide.removeAttr( 'pattern' ); + } + + $field + .attr( 'name', name + opt.visible_suffix ) + .after( $hide ); + } + + $.fn.easyObfuscatedField = function( config ) + { + config = $.extend( defaults, config ); + + var $this = $(this), + selector = $this.selector, + $body = $('body'), + observing = $body.data( observing_key ) || []; + + // do the prep work for the obfuscator + $this.each(function(){ + + prep_obfuscation( $(this), config ); + + }); + + // only set up one observer per body + if ( $.inArray( selector, observing ) > -1 ) + { + return $this; + } + + // make a note that we’ll be observing this new selector + observing.push( selector ); + + $body + // focus returns the value of the unobfuscated field (if it exists) + // it also preps the field if we did not catch it on DOM ready + .on( 'focus', selector, function(){ + + var $this = $(this), + name = $this.attr('name'), + $hidden, + val; + + if ( name.indexOf( config.visible_suffix ) < 0 ) + { + prep_obfuscation( $this ); + } + + $hidden = $( 'input[name=' + name.replace( config.visible_suffix, '' ) + ']' ); + + if ( $hidden.length && + ( val = $hidden.val() ) != '' ) + { + $this.val( val ); + } + + }) + .on( 'blur', selector, function(){ + + var $this = $(this), + val = $this.val(), + i; + + // update the hidden field + $('input[name=' + $this.attr('name').replace( config.visible_suffix, '' ) + ']') + .val(val); + + // obfuscate the visible field + if ( val.length > 3 ) + { + val = val.split(''); + i = val.length - 3; + while ( i-- ) + { + val[i] = '*'; + } + $this.val(val.join('')); + } + + }) + .data( observing_key, observing ); + + // now return + return $this; + + }; + +}(jQuery)); \ No newline at end of file