In this article I am going to explain , ASP. net text only accept valid
decimal value, it allow you to only number and one dot point.
JavaScript Code that allow valid decimal entry
Function calling from textbox
JavaScript Code that allow valid decimal entry
function fun_AllowOnlyAmountAndDot(txt) {
if (event.keyCode
> 47 && event.keyCode < 58 || event.keyCode == 46 || event.keyCode ==
44) {
var
txtbx = document.getElementById(txt);
var
amount = document.getElementById(txt).value;
var
present = 0;
var
count = 0;
if
(amount.indexOf(".", present) ||
amount.indexOf(".", present + 1));
{
// alert('0');
}
do
{
present = amount.indexOf(".",
present);
if
(present != -1) {
count++;
present++;
}
}
while
(present != -1);
if
(present == -1 && amount.length == 0 && event.keyCode == 46) {
event.keyCode = 0;
return false;
}
if
(present == -1 && amount.length == 0 && event.keyCode == 44) {
event.keyCode = 0;
//alert("Wrong position of decimal point not allowed !!");
return false;
}
if
(count >= 1 && event.keyCode == 46) {
event.keyCode = 0;
//alert("Only one decimal point is allowed !!");
return false;
}
if
(count >= 1 && event.keyCode == 44) {
event.keyCode = 0;
//alert("Only one decimal point is allowed !!");
return false;
}
if
(count == 1) {
var
lastdigits = amount.substring(amount.indexOf(".")
+ 1, amount.length);
if
(lastdigits.length >= 2) {
//alert("Two decimal places only
allowed");
event.keyCode = 0;
return false;
}
}
return
true;
}
else
{
event.keyCode = 0;
//alert("Only
Numbers with dot allowed !!");
return
false;
}
}
|
Function calling from textbox
<asp:TextBox ID="txtvalidenter" runat="server"
CssClass="inrinpt"
onkeypress="return
fun_AllowOnlyAmountAndDot(this.id);" ></asp:TextBox>
|