Hello all.
I have two questions:
1)I have a textbox in asp.net web form. I want to restrict users to enter only decimals in this textbox.
I know the JQuery code for this purpose,but how can I do this using javascipt?
This is what I did:
function isNumberKey_decimal(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
}
protected void Page_Load(object sender, EventArgs e)
{
txt1.Attributes.Add("onkeypress", "return isNumberKey_decimal(event)");
}
It works,but there is a problem. It doesn't prevent from entering different dots. For example, 10..2 or 10.. , are allowed. How can I prevent from this?
2)I have another textbox. I want to force users to enter only numbers 1 to 10. How can I do this using java script?
Please help me.
Thanks a lot.
Update:
Finally I could find the answer of my first question:
function isNumberKey_decimal(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode == 46 && evt.srcElement.value.split('.').length > 1) {
return false;
}
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
Hope it helps you.
Can anyone answer my second question please?
Thanks a million.