Regular expression.

  • Thread starter Thread starter Jack jensen
  • Start date Start date
J

Jack jensen

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
 
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);

}
 
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
 
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.
 
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.
 
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.
 
The code below has the same problem as mine...
Always matches.
For some reason i only accepts numbers as Zip codes:
 
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.
 
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)

{

......
 
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
 
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]
 

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

Back
Top