Text box will accept numeric with two decimal point.
The below JQuery function used with onkeypress attribute of textbox.
Code
isNumberKey = function (evt, obj) {
var charCode = (evt.which) ? evt.which : evt.keyCode + ".10";
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46
&& charCode != "46.10" && charCode != "37.10" && charCode != "39.10"
&& charCode != "9.10" && charCode != "35.10" && charCode != "36.10") {
return false;
}
else {
var cursorPosition = $(obj).getCursorPosition();
var dotPosition = obj.value.indexOf('.');
if (dotPosition == -1)
{
if (charCode == "9.10" || charCode == 8 || charCode == "37.10" || charCode == "39.10"
|| charCode == "35.10" || charCode == "36.10" || charCode == "46.10") {
// tab, backspace, left arrow, right arrow, home, end, delete
return true;
}
var charBeforedot = obj.value.split('.')[0].length;
var valueBeforedot = parseInt(obj.value.split('.')[0]);
if (valueBeforedot == 10 && charCode == 48) {
return true;
}
if (valueBeforedot >= 10 && charCode != 46) {
return false;
}
if (valueBeforedot >= 100 || charBeforedot >= 3) {
return false;
}
if (dotPosition < 0) {
if (charBeforedot >= 2 && charCode != 46) {
return false;
}
}
}
else if (dotPosition >= cursorPosition) {
if (dotPosition >= 0 && charCode == 46) {
return false;
}
if (charCode == "9.10" || charCode == 8 || charCode == "37.10" || charCode == "39.10"
|| charCode == "35.10" || charCode == "36.10" || charCode == "46.10") {
// tab, backspace, left arrow, right arrow, home, end, delete
return true;
}
var charBeforedot = obj.value.split('.')[0].length;
var valueBeforedot = parseInt(obj.value.split('.')[0]);
if (valueBeforedot == 10 && charCode == 48) {
var valueAfterdot = parseInt(obj.value.split('.')[1]);
if (valueAfterdot > 0) {
return false;
}
else {
return true;
}
}
if (valueBeforedot >= 10 && charCode != 46) {
return false;
}
if (valueBeforedot >= 100 || charBeforedot >= 3) {
return false;
}
}
else {
if (charCode == "9.10" || charCode == 8 || charCode == "37.10" || charCode == "39.10"
|| charCode == "35.10" || charCode == "36.10" || charCode == "46.10") {
// tab, backspace, left arrow, right arrow, home, end, delete
return true;
}
if (dotPosition >= 0) {
var valueBeforedot = parseInt(obj.value.split('.')[0]);
if (valueBeforedot >= 100 && charCode != 48)
{
return false;
}
var len = obj.value.length;
var charAfterdot = (len + 1) - dotPosition;
if (charCode == 46)
{
return false;
}
if (charAfterdot > 3) {
return false;
}
}
}
}
return true;
}
The below JQuery function used with onkeypress attribute of textbox.
Code
isNumberKey = function (evt, obj) {
var charCode = (evt.which) ? evt.which : evt.keyCode + ".10";
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46
&& charCode != "46.10" && charCode != "37.10" && charCode != "39.10"
&& charCode != "9.10" && charCode != "35.10" && charCode != "36.10") {
return false;
}
else {
var cursorPosition = $(obj).getCursorPosition();
var dotPosition = obj.value.indexOf('.');
if (dotPosition == -1)
{
if (charCode == "9.10" || charCode == 8 || charCode == "37.10" || charCode == "39.10"
|| charCode == "35.10" || charCode == "36.10" || charCode == "46.10") {
// tab, backspace, left arrow, right arrow, home, end, delete
return true;
}
var charBeforedot = obj.value.split('.')[0].length;
var valueBeforedot = parseInt(obj.value.split('.')[0]);
if (valueBeforedot == 10 && charCode == 48) {
return true;
}
if (valueBeforedot >= 10 && charCode != 46) {
return false;
}
if (valueBeforedot >= 100 || charBeforedot >= 3) {
return false;
}
if (dotPosition < 0) {
if (charBeforedot >= 2 && charCode != 46) {
return false;
}
}
}
else if (dotPosition >= cursorPosition) {
if (dotPosition >= 0 && charCode == 46) {
return false;
}
if (charCode == "9.10" || charCode == 8 || charCode == "37.10" || charCode == "39.10"
|| charCode == "35.10" || charCode == "36.10" || charCode == "46.10") {
// tab, backspace, left arrow, right arrow, home, end, delete
return true;
}
var charBeforedot = obj.value.split('.')[0].length;
var valueBeforedot = parseInt(obj.value.split('.')[0]);
if (valueBeforedot == 10 && charCode == 48) {
var valueAfterdot = parseInt(obj.value.split('.')[1]);
if (valueAfterdot > 0) {
return false;
}
else {
return true;
}
}
if (valueBeforedot >= 10 && charCode != 46) {
return false;
}
if (valueBeforedot >= 100 || charBeforedot >= 3) {
return false;
}
}
else {
if (charCode == "9.10" || charCode == 8 || charCode == "37.10" || charCode == "39.10"
|| charCode == "35.10" || charCode == "36.10" || charCode == "46.10") {
// tab, backspace, left arrow, right arrow, home, end, delete
return true;
}
if (dotPosition >= 0) {
var valueBeforedot = parseInt(obj.value.split('.')[0]);
if (valueBeforedot >= 100 && charCode != 48)
{
return false;
}
var len = obj.value.length;
var charAfterdot = (len + 1) - dotPosition;
if (charCode == 46)
{
return false;
}
if (charAfterdot > 3) {
return false;
}
}
}
}
return true;
}