Validating a text box

  • Thread starter Thread starter JJ297
  • Start date Start date
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
 
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
 
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.
 
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?
 
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.
 
Back
Top