Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Regular Expression for validation!

    Quote Originally Posted by harishrajasekaran View Post
    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.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default 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. #5
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default 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. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. validation checking using regular expression...
    By Debarpita Paul in forum New To Java
    Replies: 4
    Last Post: 01-08-2013, 06:31 PM
  2. Regular expression
    By Krik in forum New To Java
    Replies: 3
    Last Post: 10-26-2012, 05:06 PM
  3. Regular expression
    By garnaout in forum New To Java
    Replies: 4
    Last Post: 05-15-2012, 06:22 PM
  4. Regular Expression Help
    By niketanand in forum Advanced Java
    Replies: 1
    Last Post: 06-24-2011, 04:56 PM
  5. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM

Posting Permissions

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