Shabarinath Volam
Shabarinath Volam

Reputation: 745

Regular expression for mobile number validation

Hi I need to validate mobile number based on the following criteria

So I have tried using the following regular expression

([0-9]\\s*){8}

But its not working. Can any one help me?

Upvotes: 0

Views: 484

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

You're not far off (if you really want to follow these criteria):

^[=+\s]*(?:[0-9][=+\s]*){8,}$

Explanation:

^        # Start of string
[=+\s]*  # Optionally match =, + or whitespace
(?:      # Start of group:
 [0-9]   # Match a digit
 [=+\s]* # Optionally match =, + or whitespace
){8,}    # Repeat at least 8 times
$        # End of string

Don't forget to double the backslashes in a Java string:

Pattern regex = Pattern.compile("^[=+\\s]*(?:[0-9][=+\\s]*){8,}$");

But of course these rules also permit matches like ++++0+0===0=+0+000+0+0+0+0+++...

Upvotes: 2

Related Questions