Link to home
Start Free TrialLog in
Avatar of feesu
feesu

asked on

ASP.NET 2005 - How can I force the user to enter only numbers into a text box?

Hi,

I want to have two text boxes, one is for passport number, and has to accept only numbers (no symbols, or dots or alphanumeric)

And the other one is for the name, accepts only Alphabets (no numbers, symbols etc...)

I want to implement this on key press! Any ready functions?
Avatar of VikramMullick
VikramMullick

In the TextBoxes , there is property for SkinID. Set it as per your needs.
in javascript, there are many things you can do... like the following:

If(IsNaN(document.getElementById("textbox").value)=true)
{
       alert("Please enter only numbers for the passport!");
       document.getElementById("textbox").value=document.getElementById("textbox").value.substring(0,document.getElementById("textbox").value.length-1)
}

this will alert not to use letters, and remove the invalid character, onKeyUp...

but if someone holds the key down, you're screwed... so the best way would be to loop through all characters in the textbox, and if one is not a number, remove it, and alert the user if any non numerical numbers were found, and with this function type (loop), you can also check for any @non-wanted@ characters... like a dot, !, etc...

If(IsNaN(document.getElementById("textbox").value)==true)
ASKER CERTIFIED SOLUTION
Avatar of channa_m
channa_m
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you use ajax, you can use the Masked Edit Text box:

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx