Skip to content

Instantly share code, notes, and snippets.

@arlg
Last active December 12, 2015 09:48
Show Gist options
  • Save arlg/4753833 to your computer and use it in GitHub Desktop.
Save arlg/4753833 to your computer and use it in GitHub Desktop.
Input functions
//Clears the input on focus, restore its initial content on blur if nothing entered by user
var input = $('input[type=text]');
input.focus(function() {
var el = $(this);
if(el.val() === el.attr('data-placeholder')){
$(this).val('');
}
}).blur(function() {
var el = $(this);
if(el.val() === ''){
el.val(el.attr('data-placeholder'));
}
});
//Allows only numerical value
//Numerical input
$(".numerical").keypress(function(event){
var theEvent = event || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9]|\./;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment