percentage 5 decimals

by hamzeen hameem

HTML

<form action="#">
  Enter Percentage: <input type="text" name="decimal" id="decimal" maxlength="9"/>
</form>

CSS

form{
background-color:#eee;
margin:20px;
padding:10px;    
}

#error{
margin:5px;
background-color:#bc0000;
color:#FFFFFF;
display:block;
}

JavaScript

$(document).ready(function() {
	$('input#decimal').blur(function() {
  	var tailoredNum = parseFloat($(this).val());
    if(!$(this).val()) {
    	return;
    }

		if(parseFloat($(this).val()) >= 100.00000) {
      tailoredNum = 100;
		} else if(parseFloat($(this).val()) <= 0.00000) {
      tailoredNum = 0;
		}
		$(this).val(tailoredNum.toFixed(5));
	});
  
	$("input#decimal").keydown(function (e) {
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 || 
        (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) || 
        (e.keyCode == 67 && (e.ctrlKey === true || e.metaKey === true)) || 
        (e.keyCode == 88 && (e.ctrlKey === true || e.metaKey === true)) || 
        (e.keyCode >= 35 && e.keyCode <= 39)) {
        	return;
        }

				// Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
	});
});