English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hello
I want to allow only numeric field to be entred into textbox. How can I do that in C#.Net2,ASP.Net2 or Javascript.

thanks

2007-03-04 04:15:51 · 6 answers · asked by Jack J 1 in Computers & Internet Programming & Design

6 answers



In your code, simply write:

txtTextBoxId.Attributes _
.Add("onKeyDown", _
"return maskBox(event);")

This will work in IE, FF, Opera, and most others. For the few that don't understand it, it will just let them type anything; won't return an error. Sorry for the line breaks, Yahoo seems to be hiding code if I don't break it up.

2007-03-04 05:10:30 · answer #1 · answered by Rex M 6 · 0 1

The easiest way is to use a Range Validator control.




Notice: In order to make the field required, you have to add a Required Field validator. i.e. the code above considers that an empty textbox has valid data.

Hope this helps

2007-03-04 05:44:26 · answer #2 · answered by Smutty 6 · 0 1

In VB, you can use the KEYPRESS event of the textbox to restrict the ASCII characters to between certain values. Not quite sure how to do that in C#, but that should give you a startign point.

2007-03-04 15:33:49 · answer #3 · answered by Richard H 7 · 0 0

You can do it easily via asp or javascript.
Just you need the ASCII code of a key.
So you can get them via textpad.Textpad has all of them on the left side panel.
Find the key code(number) from 0 to 9.
Then you should write some code for Text_Changed event of the textbox.Just you should unhandle the key press function while a number is not entered.

If(e.KeyCodenum2)
{
e.Handled=false;
}

2007-03-04 09:29:20 · answer #4 · answered by Moein 3 · 0 1

You validate the text using a regular expression.
I believe the regex for non-numeric is \D .

So any match for \D means there is non-numeric in the string.

2007-03-04 05:11:55 · answer #5 · answered by Raymond 3 · 0 1

one way is to apply a RangeValidator administration as follows. you are able to omit the mandatory field validator:

2016-12-05 05:42:42 · answer #6 · answered by winkles 4 · 0 0

fedest.com, questions and answers