Monday, 11 June 2018

How to change integer value in number format jQuery

Here are simple way you can change integer value in number format on key up using jQuery

Html Code:
<input type="text" name="amount" class="required" value="">

jQuery Code:
<script>
jQuery.noConflict();
jQuery(document).ready( function($){ 
$(document).on('keyup', 'input.required', function(event){
if($(this).hasClass('field-error')){
$(this).css('border-color', '#83A4C5');
$(this).removeClass('field-error');
}   
var selection = window.getSelection().toString();
if ( selection !== '' ) {
return;
}           
// When the arrow keys are pressed, abort it.
if ( $.inArray( event.keyCode, [38,40,37,39] ) !== -1 ) {
return;
}      
var $this = $( this );           
// Get the value.
var input = $this.val();            
var input = input.replace(/[\D\s\._\-]+/g, "");
input = input ? parseInt( input, 10 ) : 0;
$this.val( function() {
return ( input === 0 ) ? "" : input.toLocaleString( "en-US" );
} );      
});
});
</script>

2 comments: