views:

77

answers:

3

I need a regular expression to validate the mobile number up to 9 digits, if the telephone number starts with 8 otherwise 10 digits needs to be entered. The numbers must start either with 9 or 8 with above criteria.

+2  A: 

Not sure about the answer, but you should check out this very useful website for reg expressions incase you are interested:

http://www.regexlib.com

http://www.regexlib.com/CheatSheet.aspx

VoodooChild
http://www.regular-expressions.info/ is also an invaluable resource.
Johnsyweb
+3  A: 
^(8\d{8}|[1-79]\d{9})$
Amarghosh
your expression is also accepting numbers statring with 1 ,2,3 ..so on. I need numbers starting with 8 or 9 with the above condition
skamale
No, it behaves exactly as you specified.
Tim Pietzcker
@skamale You didn't say anything special about numbers starting with 9. This regex matches only nine digit strings starting with 8 and ten digit strings starting with numbers other than 0 and 8. If that's not what you want, edit the question's wording to clarify it. Add a couple of examples of what should and should not be accepted.
Amarghosh
A: 

Use the following regular expression to match the phone numbers starting with 8 or 9.

^8|9(8\d{8}|[1-79]\d{9})$
rekha_sri