471,317 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Regular expression.


Hello,
I need to validate the input of my text box.
I want the users to enter only numbers.
Wich regular expression can i use with my Regex class?

many thanks
Jack
Nov 29 '05 #1
13 1239
use \d
or \d+ if you want 1 or more digits

"Jack jensen" <Ja*********@mail.dk> a écrit dans le message de
news:uV**************@TK2MSFTNGP15.phx.gbl...

Hello,
I need to validate the input of my text box.
I want the users to enter only numbers.
Wich regular expression can i use with my Regex class?

many thanks
Jack

Nov 29 '05 #2
thanks,

i wrote the code below but it always enters the error block no matter what i
write.
what i'm doing wrong?
string exp = @"\d+";

Regex num = new Regex(exp);

String s =tbOPostalCode.Text;

Match m = num.Match(s);

if (! m.Success )

{

MessageBox.Show("The zip code must be a number.", "Zip code error",

MessageBoxButtons.OK, MessageBoxIcon.Error);

}


Nov 29 '05 #3
Here is a method that I use in my applications

public static bool IsNumeric(string toValidate)
{
Regex pattern = new Regex(@"[^\d]");
return !pattern.IsMatch(toValidate);
}

Also zip codes aren't always numeric. What if the user inputed
55555-2345

Nov 29 '05 #4
use this :

if (!Regex.IsMatch(tbOPostalCode.Text,@"\d+")) {
MessageBox.Show("The zip code must be a number.", "Zip code error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Hope it helps,

Ludovic SOEUR.

"Jack jensen" <Ja*********@mail.dk> a écrit dans le message de
news:e7*************@TK2MSFTNGP09.phx.gbl...
thanks,

i wrote the code below but it always enters the error block no matter what i write.
what i'm doing wrong?
string exp = @"\d+";

Regex num = new Regex(exp);

String s =tbOPostalCode.Text;

Match m = num.Match(s);

if (! m.Success )

{

MessageBox.Show("The zip code must be a number.", "Zip code error",

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

Nov 29 '05 #5

You could create your own custom text box.....

namespace football
{
public class HexTextBox : TextBox
{
public HexTextBox()
{
}

protected override void OnKeyPress(KeyPressEventArgs e)
{

if(Char.IsNumber(e.KeyChar))
{
Convert.ToInt32(e.KeyChar);
}

else e.Handled=true;

}
}
}

override the onKeyPress event and if the key pressed is a character deal
with it else ignore it.

"Ludovic SOEUR" wrote:
use this :

if (!Regex.IsMatch(tbOPostalCode.Text,@"\d+")) {
MessageBox.Show("The zip code must be a number.", "Zip code error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Hope it helps,

Ludovic SOEUR.

"Jack jensen" <Ja*********@mail.dk> a écrit dans le message de
news:e7*************@TK2MSFTNGP09.phx.gbl...
thanks,

i wrote the code below but it always enters the error block no matter what

i
write.
what i'm doing wrong?
string exp = @"\d+";

Regex num = new Regex(exp);

String s =tbOPostalCode.Text;

Match m = num.Match(s);

if (! m.Success )

{

MessageBox.Show("The zip code must be a number.", "Zip code error",

MessageBoxButtons.OK, MessageBoxIcon.Error);

}


Nov 29 '05 #6
I disagree with you, James.
What about Ctrl+C, Ctrl+V, and so on ? You could paste something without
digits.
You have to check each time the text is modified in the validating event
that is done for that.

"James L" <Ja****@discussions.microsoft.com> a écrit dans le message de
news:71**********************************@microsof t.com...

You could create your own custom text box.....

namespace football
{
public class HexTextBox : TextBox
{
public HexTextBox()
{
}

protected override void OnKeyPress(KeyPressEventArgs e)
{

if(Char.IsNumber(e.KeyChar))
{
Convert.ToInt32(e.KeyChar);
}

else e.Handled=true;

}
}
}

override the onKeyPress event and if the key pressed is a character deal
with it else ignore it.

"Ludovic SOEUR" wrote:
use this :

if (!Regex.IsMatch(tbOPostalCode.Text,@"\d+")) {
MessageBox.Show("The zip code must be a number.", "Zip code error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Hope it helps,

Ludovic SOEUR.

"Jack jensen" <Ja*********@mail.dk> a écrit dans le message de
news:e7*************@TK2MSFTNGP09.phx.gbl...
thanks,

i wrote the code below but it always enters the error block no matter
what i
write.
what i'm doing wrong?
string exp = @"\d+";

Regex num = new Regex(exp);

String s =tbOPostalCode.Text;

Match m = num.Match(s);

if (! m.Success )

{

MessageBox.Show("The zip code must be a number.", "Zip code error",

MessageBoxButtons.OK, MessageBoxIcon.Error);

}


Nov 29 '05 #7
true, but its a laugh creating your own text boxes.

"Ludovic SOEUR" wrote:
I disagree with you, James.
What about Ctrl+C, Ctrl+V, and so on ? You could paste something without
digits.
You have to check each time the text is modified in the validating event
that is done for that.

"James L" <Ja****@discussions.microsoft.com> a écrit dans le message de
news:71**********************************@microsof t.com...

You could create your own custom text box.....

namespace football
{
public class HexTextBox : TextBox
{
public HexTextBox()
{
}

protected override void OnKeyPress(KeyPressEventArgs e)
{

if(Char.IsNumber(e.KeyChar))
{
Convert.ToInt32(e.KeyChar);
}

else e.Handled=true;

}
}
}

override the onKeyPress event and if the key pressed is a character deal
with it else ignore it.

"Ludovic SOEUR" wrote:
use this :

if (!Regex.IsMatch(tbOPostalCode.Text,@"\d+")) {
MessageBox.Show("The zip code must be a number.", "Zip code error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Hope it helps,

Ludovic SOEUR.

"Jack jensen" <Ja*********@mail.dk> a écrit dans le message de
news:e7*************@TK2MSFTNGP09.phx.gbl...
> thanks,
>
> i wrote the code below but it always enters the error block no matter what i
> write.
> what i'm doing wrong?
>
>
> string exp = @"\d+";
>
> Regex num = new Regex(exp);
>
> String s =tbOPostalCode.Text;
>
>
>
> Match m = num.Match(s);
>
> if (! m.Success )
>
> {
>
> MessageBox.Show("The zip code must be a number.", "Zip code error",
>
> MessageBoxButtons.OK, MessageBoxIcon.Error);
>
> }
>
>
>
>


Nov 29 '05 #8

Hi Jack,

Regular expressions can be daunting to write. We're lucky that Roy Osherove
provided an excellent regular expressions testing and learning tool called
The Regulator. Get your copy here http://regulator.sf.net/

Best regards,
Paul Gielens

Visit my blog @ http://weblogs.asp.net/pgielens/
###

Nov 29 '05 #9

The code below has the same problem as mine...
Always matches.
For some reason i only accepts numbers as Zip codes:


if (!Regex.IsMatch(tbOPostalCode.Text,@"\d+")) {
MessageBox.Show("The zip code must be a number.", "Zip code error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Hope it helps,

Ludovic SOEUR.

"Jack jensen" <Ja*********@mail.dk> a écrit dans le message de
news:e7*************@TK2MSFTNGP09.phx.gbl...
thanks,

i wrote the code below but it always enters the error block no matter
what

i
write.
what i'm doing wrong?
string exp = @"\d+";

Regex num = new Regex(exp);

String s =tbOPostalCode.Text;

Match m = num.Match(s);

if (! m.Success )

{

MessageBox.Show("The zip code must be a number.", "Zip code error",

MessageBoxButtons.OK, MessageBoxIcon.Error);

}


Nov 30 '05 #10
With the code I provided, it works on my computer.

It works with the following code :
tbOPostalCode.Text="310215"
if (!Regex.IsMatch(tbOPostalCode.Text,@"\d+")) {
MessageBox.Show("The zip code must be a number.", "Zip code error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

with tbOPostalCode.Text="310 215" : it shows the message
with tbOPostalCode.Text="310215" : it doesn't show anything

Put your entire code because there are no reason why it doesn't work on your
computer.

Ludovic SOEUR.
"Jack jensen" <Ja*********@mail.dk> a écrit dans le message de
news:uI*************@TK2MSFTNGP09.phx.gbl...

The code below has the same problem as mine...
Always matches.
For some reason i only accepts numbers as Zip codes:


if (!Regex.IsMatch(tbOPostalCode.Text,@"\d+")) {
MessageBox.Show("The zip code must be a number.", "Zip code error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Hope it helps,

Ludovic SOEUR.

"Jack jensen" <Ja*********@mail.dk> a écrit dans le message de
news:e7*************@TK2MSFTNGP09.phx.gbl...
thanks,

i wrote the code below but it always enters the error block no matter
what

i
write.
what i'm doing wrong?
string exp = @"\d+";

Regex num = new Regex(exp);

String s =tbOPostalCode.Text;

Match m = num.Match(s);

if (! m.Success )

{

MessageBox.Show("The zip code must be a number.", "Zip code error",

MessageBoxButtons.OK, MessageBoxIcon.Error);

}



Nov 30 '05 #11
Oh, it looks like tbOPostalCode.Text is always empty....
It should not be as i'm handling it in the validating event....
I will try sender.ToString() instead.
private void tbPostalCode_Validating(object sender,
System.ComponentModel.CancelEventArgs e)

{

......

with tbOPostalCode.Text="310 215" : it shows the message
with tbOPostalCode.Text="310215" : it doesn't show anything

Put your entire code because there are no reason why it doesn't work on
your
computer.

Ludovic SOEUR.
"Jack jensen" <Ja*********@mail.dk> a écrit dans le message de
news:uI*************@TK2MSFTNGP09.phx.gbl...

The code below has the same problem as mine...
Always matches.
For some reason i only accepts numbers as Zip codes:


> if (!Regex.IsMatch(tbOPostalCode.Text,@"\d+")) {
> MessageBox.Show("The zip code must be a number.", "Zip code error",
> MessageBoxButtons.OK, MessageBoxIcon.Error);
> }
>
> Hope it helps,
>
> Ludovic SOEUR.
>
>
>
> "Jack jensen" <Ja*********@mail.dk> a écrit dans le message de
> news:e7*************@TK2MSFTNGP09.phx.gbl...
>> thanks,
>>
>> i wrote the code below but it always enters the error block no matter
>> what
> i
>> write.
>> what i'm doing wrong?
>>
>>
>> string exp = @"\d+";
>>
>> Regex num = new Regex(exp);
>>
>> String s =tbOPostalCode.Text;
>>
>>
>>
>> Match m = num.Match(s);
>>
>> if (! m.Success )
>>
>> {
>>
>> MessageBox.Show("The zip code must be a number.", "Zip code error",
>>
>> MessageBoxButtons.OK, MessageBoxIcon.Error);
>>
>> }
>>
>>
>>
>>
>
>



Nov 30 '05 #12

private void tbPostalCode_Validating(object sender,
System.ComponentModel.CancelEventArgs e)

{
......
You were right i had to use sender.ToString() ... I don't know why the
textbox field is empty?

Many thanks

JJ
Nov 30 '05 #13
Jack,

I'd suggest to take a look at this tool:

http://regex.osherove.com/

It is a tool for building and testing regular expressions. It can be used to
find different pre-build regexes.

HTH
Stoitcho Goutsev (100) [C# MVP]

"Jack jensen" <Ja*********@mail.dk> wrote in message
news:uV**************@TK2MSFTNGP15.phx.gbl...

Hello,
I need to validate the input of my text box.
I want the users to enter only numbers.
Wich regular expression can i use with my Regex class?

many thanks
Jack

Nov 30 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Buddy | last post: by
4 posts views Thread by Neri | last post: by
11 posts views Thread by Dimitris Georgakopuolos | last post: by
3 posts views Thread by James D. Marshall | last post: by
7 posts views Thread by Billa | last post: by
9 posts views Thread by Pete Davis | last post: by
25 posts views Thread by Mike | last post: by
1 post views Thread by NvrBst | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.