-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
947d21b
commit 135d0c5
Showing
3 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); |