Gyða

Ok so I got a string of f.expl. a phone number and I want to check that it's just numbers (also have a security number with all kind of calculations, e-mail and so forth). I know this would be easy in Jave but I'm using C#.

I figured I would just get length and then run through the string with some kind of for loop. But frankly have no idea how to do it. Is there some kind of get next character for string member in C#


P.S pardon my spelling English, it is not my first language .



Re: Visual C# General Checking a string

Vaughn Friesen

An easier way would be to use a masked text box, http://msdn2.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx.



Re: Visual C# General Checking a string

John.Doe

As always when it comes to checking like that stuff.... use regular expressions, e.g. for checking if a string is just numbers:

Regex regex = new Regex("^[0-9]+$");
Console.WriteLine(regex.Match(text).Success);

For more complicated things like, e.g. if a string is an email address:

Regex regex = new Regex("^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");
Console.WriteLine(regex.Match(text).Success);

You can find a lot of useful regular expressions here:

http://msdn2.microsoft.com/en-us/library/ms998267.aspx





Re: Visual C# General Checking a string

OmegaMan

Also check out the MSDN's .Net Regular Expression forum for help with regex'es and the top announcement .Net Regex Resource Reference which has information geared towards all levels, expert to beginner.