Textbox taking only number

microkarl

Regular
Joined
Apr 22, 2004
Messages
88
Location
Morristown, NJ
All,
How can I have only number being input in the textbox if I have a textbox like this:
Code:
<asp:textbox id="txtCardNumber" runat="server" MaxLength="20" size="25" />

I tried [0-9]{1,20} and /d{1,} but it won;t work as long as you have the first character as number.

Thanks
 
This might work

microkarl said:
All,
How can I have only number being input in the textbox if I have a textbox like this:
Code:
<asp:textbox id="txtCardNumber" runat="server" MaxLength="20" size="25" />

I tried [0-9]{1,20} and /d{1,} but it won;t work as long as you have the first character as number.

Thanks

For this to work, when using .NET regular expressions use the "multi-line" setting.

Then use your regular expression with beginning and end of line anchors like this:

^[0-9]{1,20}$

This says that the string must match:
* the beginning of the line ^
* 1-20 numbers [0-9]{1,20}
* then end of the line $

Basically, the input string must start with a number and contain nothing but 1-20 numbers until the end.

If your textbox is not a multi-line textbox itself, then you don't need to specify multi-line during the regex test. Please see this link for descriptions of .NET regex options. :)
 
Back
Top