Regular expression validation


I am working on web application VS2003 using C#

I wish to do validate a field in a form using regular expression

The text in the field should be of minimum length 6 and maximum 15 and all should be numeric

Please guide in writting regular expression

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
b0lsc0ttIT ManagerCommented:

I am not a C# expert but assuming you have imported the regex library you should just need something like ...

bool foundMatch = false;
try {
      foundMatch = Regex.IsMatch(subjectString, @"^\d{6,15}$");
} catch (ArgumentException ex) {
      // Syntax error in the regular expression

That assumes the field's value is in the variable subjectString.  The expression is just a little different from the one Ozo suggested but you could easily use his in my code.

Let me know if you have any questions or need more information.

tia_kamakshiAuthor Commented:
Thanks it works fine with numeric test, but I am sorry for wrong question. Basically I want that text should contain numeric and dots(s) and dots should be there

and the length should be 6 and 15

We have to validate that there should be dot (.) present in the code. Is it possible that we can check that there should be atleast 2 dots available in the code

My error message will be

Code must only contain numbers and dots(.) and be between 6 and 15 characters

I am sorry for wrong question and late reply

Awaiting your ans

b0lsc0ttIT ManagerCommented:
That is quite different.  The expression below should do this then ...

      foundMatch = Regex.IsMatch(subjectString, @"(?=\d*\.\d*\.\d*)^[\d.]{6,15}$");

It will make sure there are at least 2 dots and validate only something with numeric or a dot between 6-15 characters long.

Let me know if you have a question or need more info.

b0lsc0ttIT ManagerCommented:
After your last comment I find the way you closed this very puzzling.  Oh well ... you probably just decided to award for the comment that answered your original question.  If you open a new one for what you just asked then let me know.  The expression I just posted should do it.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.