Regular expression validation

Hi,

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

Thanks
tia_kamakshiAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ozoConnect With a Mentor Commented:
^[0-9]{6,15}$
0
 
b0lsc0ttIT ManagerCommented:
tia_kamakshi,

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.

b0lsc0tt
0
 
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


Thanks
0
 
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.

bol
0
 
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.

bol
0
All Courses

From novice to tech pro — start learning today.