Regular Expression With Mask
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?
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 :)
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}$
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_-____
this expression will work for you ^[( ][0-9][1-9][0-9][) ]?[0-9]{3}[- ]?[0-9]{4}$ this expression defines that except second digit of phone number every digit will consider {0}
精彩评论