Validating a text box

by JJ297 on 11/1/2007 2:10:00 AM How do I validate a text box field which only requires a number in it?

 

Re: Validating a text box

by Rad [Visual C# MVP] on 11/2/2007 3:50:00 AM On Thu, 01 Nov 2007 09:09:55 -0700, JJ297 <nc297@yahoo.com> wrote:

>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

--
http://bytes.thinkersroom.com
 

Re: Validating a text box

by Mansi Shah on 11/1/2007 3:06:00 PM
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[i]).ToString());
       if (iAsciiCode != 46)
       {
          if (!((iAsciiCode > 47) && (iAsciiCode < 58)))
          {
            //some message that it should be number
            return;
          }
       }
    }
}


Regards,
Mansi Shah.

*** Sent via Developersdex http://www.developersdex.com ***
 

Re: Validating a text box

by JJ297 on 11/2/2007 1:56:00 AM On Nov 1, 7:15 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
> 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
>
>
>
> "JJ297" wrote:
> > How do I validate a text box field which only requires a number in it?- Hide quoted text -
>
> - 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?

 

Re: Validating a text box

by JJ297 on 11/2/2007 2:11:00 AM On Nov 2, 11:55 am, JJ297 <nc...@yahoo.com> wrote:
> On Nov 1, 7:15 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
> wrote:
>
>
>
>
>
> > 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
>
> > "JJ297" wrote:
> > > How do I validate a text box field which only requires a number in it?- Hide quoted text -
>
> > - 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?- Hide quoted text -
>
> - Show quoted text -

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