# Thread: Regular Expression for validation!

1. Member
Join Date
Aug 2013
Posts
3
Rep Power
0

## Regular Expression for validation!

Hello everyone,

I'm trying to form a regular expression for a form validation where I would accept Alphanumeric characters and a maximum of 3 special characters which includes only (. ' ` -)

I came up with this -
[A-Za-z0-9]*[-. '`]{0,3}

But this won't allow me to add special characters at the beginning. Example it would fail for this string : "-sampletext-"

So if I go this way -
[A-Za-z0-9]*[-. '`]{0,3}[A-Za-z0-9]*

I would solve the above case, but still it would fail as in for the string : "-sample-sample-"

Could someone help me with this where I can enter the alphanumeric and special characters (max of 3) at random places. I gave a try with OR operators - not very lucky. May be I'm wrong.

Thanks a lot for your help guys.

2. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,696
Rep Power
4

## Re: Regular Expression for validation!

Well, I used these two patterns.
Java Code:
```String sp = "([-`'.]{0,3})"; // subpattern
String regex = sp + "[\\w&&[^_]]*" + sp + "[\\w&&[^_]]*" + sp;```
Then I just did regular matches on the regex. When a match was found, I just summed the string lengths of the capture group to check that it was less than 4. Note that in my version, three special characters will match with no alphanumerics present. This worked in all cases I checked (but it only takes one for failure).

I suspect, it would be more straight forward to just write a method checking for alphanumerics in combination with a fall thru switch statement for your special characters. As soon as the special characters exceed 3, then the validation fails.

Regards,
Jim

3. ## Re: Regular Expression for validation!

Originally Posted by harishrajasekaran
I'm trying to form a regular expression for a form validation where I would accept Alphanumeric characters and a maximum of 3 special characters which includes only (. ' ` -)
-- do you allow "(.)" ?

Maybe something like
Java Code:
`replaceAll(input, "[^(. ' ` -)]").length() < 3  && input.matches("[a-zA-Z0-9(. ' ` -)]*");`
Check whether any of your special characters need escaping inside a character class.

4. Member
Join Date
Aug 2013
Posts
3
Rep Power
0

## Re: Regular Expression for validation!

Thank you for your response Jim.
I tried your approach too. Where even if I give the string -

"---sample---sample---"

It would allow it even with 9 special characters. But the first condition is to stop the customer's from entering more than 3 special characters. Am I making sense?

5. Member
Join Date
Aug 2013
Posts
3
Rep Power
0

## Re: Regular Expression for validation!

Would love to finish this off with Java code. But we've a common validation module that accepts only regular expressions for each field. So once you form a regular expression and associate it with that field, it would take care of validating that. By field I mean firstname or address line 1 or address line 2 etc. But keeping your approach in mine, can I add an OR (||) conditions to my regular expression? Let me know what you think about it.

Thank you for your response dude.

6. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,696
Rep Power
4

## Re: Regular Expression for validation!

But you also have to sum the String lengths of the capture groups which would have invalidated the match.

Regards,
Jim

7. ## Re: Regular Expression for validation!

Would the following be of any help? Let rc be the regular expression of one regular character, let sc be the same for the special characters; you want this rc*sc?rc*sc?rc*sc?rc*

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•