Validating a text box

R

Rad [Visual C# MVP]

How do I validate a text box field which only requires a number in it?

Couple of options
1) Use a regular expression to validate the text \d+
2) Don't use a textbox at all -- use something like a numeric up down
 
G

Guest

Hi there,

Use RangeValidator with Type set to "Integer". You can also specify min and
max values as well by changing MinimumValue & MaximumValue properties
respectively. Note validation succeeds if the input control is empty. Apply
RequiredFieldValidator control to make the input control a mandatory field.

Hope this helps
 
M

Mansi Shah

Hi,

The other option is to convert all the characters of textbox to ascii
and check whether they are numbers or not.
Here is an example,


string tempString = Textbox1.Text.ToString().Trim();
if ((tempString != "") && (tempString != null))
{
for (int i = 0; i < tempString.ToString().Length; i++)
{
int iAsciiCode = Convert.ToInt32(((int)tempString).ToString());
if (iAsciiCode != 46)
{
if (!((iAsciiCode > 47) && (iAsciiCode < 58)))
{
//some message that it should be number
return;
}
}
}
}


Regards,
Mansi Shah.
 
J

JJ297

Hi there,

Use RangeValidator with Type set to "Integer". You can also specify min and
max values as well by changing MinimumValue & MaximumValue properties
respectively. Note validation succeeds if the input control is empty. Apply
RequiredFieldValidator control to make the input control a mandatory field.

Hope this helps
--
Milosz





- Show quoted text -

Thanks Milosz

I got it to work but when I enter in number all of my validation comes
up on the page how can I just get the error to say enter a number to
only come up and not the other messages?
 
J

JJ297

Thanks Milosz

I got it to work but when I enter in number all of my validation comes
up on the page how can I just get the error to say enter a number to
only come up and not the other messages?- Hide quoted text -

- Show quoted text -

I got it working by using a regular expression validator instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top