tags:

views:

100

answers:

3

I have a regular expression for phone numbers as follows: ^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$

I have a mask on the phone number textbox in the following format: (___)___-____

How can I modify the regular expression so that it accommodates the mask?

+1  A: 

Your question is a little unclear; if you want a regular expression that matches that mask, it's:

^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$
Michael Mrozek
+1  A: 

The expression for the (placeholder) mask is

^\(_{3}\)_{3}-_{4}$


The expression for a valid phone number is

^\(\d{3}\)\d{3}-\d{4}$


The mask uses _ in place of digits, so you'll need to use [\d_] as your character class to match as the user is typing.

^\([\d_]{3}\)[\d_]{3}-[\d_]{4}$


Obviously, if the user switches fields, you'll want to return an error if your phone field as any remaining _ in it. phone.match(/_/) == null should do the trick here :)

macek
+1  A: 

ValidationExpression="\([2-9]\d{2}\)\d{3}-\d{4}$|^\(_{3}\)\ _{3}-_{4}$"

This will validate the mask and (234)432-4322 but won't allow the user to enter (434)88_-__

Drew Bomb