Following Code Snippet will make validation, which allows only Floationg Point Numer On user Key-up Event Using jQuery.
Same result with Prevent user to type other than Floating Chars by using Key Press event,
$(function(){ $('.float-input').keyup(function(e){ var entered_value = $(this).val(); var regexPattern = /^\d{0,8}(\.\d{1,2})?$/; //Allow only Number as well 0nly 2 digit after dot(.) if(regexPattern.test(entered_value)) { $(this).css('background-color', 'white'); $('.err-msg').html(''); } else { $(this).css('background-color', 'red'); $('.err-msg').html('Enter a valid Decimal Number'); } }); });
$('.float-input').keypress(function(event) { if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) { event.preventDefault(); } else { var entered_value = $(this).val(); var regexPattern = /^\d{0,8}(\.\d{1,2})?$/; if(regexPattern.test(entered_value)) { $(this).css('background-color', 'white'); $('.err-msg').html(''); } else { $(this).css('background-color', 'red'); $('.err-msg').html('Enter a valid Decimal Number'); } } });