Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - How do I create a mask for a phone #?

Reply
 
Old 01-17-2009   #1 (permalink)
John Quinn


 
 

How do I create a mask for a phone #?

Did this a while back, but I forgot how to do it again.

I want to have the text box display (nnn)nnn-nnnn, either before or after
the data is entered.

bbb bbb bbbb or bbbbbbbbbb * b represents a blank or a space

If we enter 682 777 3569 or 6827773569 or 682-777-3569 I want it to look like

(682)777-3569.

Appreciate the assistance.

JZQ




My System SpecsSystem Spec
Old 01-17-2009   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: How do I create a mask for a phone #?


"John Quinn" <JohnQuinn@xxxxxx> wrote in message
news:78FA7F5A-3D40-4307-8F83-C6E8D5E726B7@xxxxxx
Quote:

> Did this a while back, but I forgot how to do it again.
>
> I want to have the text box display (nnn)nnn-nnnn, either before or after
> the data is entered.
>
> bbb bbb bbbb or bbbbbbbbbb * b represents a blank or a space
>
> If we enter 682 777 3569 or 6827773569 or 682-777-3569 I want it to look
> like
>
> (682)777-3569.
>
> Appreciate the assistance.
>
> JZQ
One way is to use the "replace" function to remove all spaces and dashes,
followed by your own function to add the paretheses and the dash.


My System SpecsSystem Spec
Old 01-17-2009   #3 (permalink)
James Whitlow


 
 

Re: How do I create a mask for a phone #?

"John Quinn" <JohnQuinn@xxxxxx> wrote in message
news:78FA7F5A-3D40-4307-8F83-C6E8D5E726B7@xxxxxx
Quote:

> Did this a while back, but I forgot how to do it again.
>
> I want to have the text box display (nnn)nnn-nnnn, either before or after
> the data is entered.
>
> bbb bbb bbbb or bbbbbbbbbb * b represents a blank or a space
>
> If we enter 682 777 3569 or 6827773569 or 682-777-3569 I want it to look
> like
>
> (682)777-3569.
>
> Appreciate the assistance.
If you don't mind instantiating the 'VBScript.RegExp' object, you can try
the below code. It will handle the examples you gave, but not international
numbers.

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MsgBox PhoneMask("682 777 3569")
MsgBox PhoneMask("6827773569")
MsgBox PhoneMask("682-777-3569")

Function PhoneMask(ByVal sPhone)
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "([^0-9])" 'Pattern to remove all non-digits
sPhone = .Replace(sPhone, Empty)
.Pattern = "(\d{3})(\d{3})(\d{4})" 'Pattern to format the digits
PhoneMask = .Replace(sPhone, "($1) $2-$3")
End With
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



My System SpecsSystem Spec
Old 01-17-2009   #4 (permalink)
Al Dunbar


 
 

Re: How do I create a mask for a phone #?


"James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
news:%23NHw6vNeJHA.3776@xxxxxx
Quote:

> "John Quinn" <JohnQuinn@xxxxxx> wrote in message
> news:78FA7F5A-3D40-4307-8F83-C6E8D5E726B7@xxxxxx
Quote:

>> Did this a while back, but I forgot how to do it again.
>>
>> I want to have the text box display (nnn)nnn-nnnn, either before or after
>> the data is entered.
>>
>> bbb bbb bbbb or bbbbbbbbbb * b represents a blank or a space
>>
>> If we enter 682 777 3569 or 6827773569 or 682-777-3569 I want it to look
>> like
>>
>> (682)777-3569.
>>
>> Appreciate the assistance.
>
> If you don't mind instantiating the 'VBScript.RegExp' object, you can try
> the below code. It will handle the examples you gave, but not
> international numbers.
>
> '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> MsgBox PhoneMask("682 777 3569")
> MsgBox PhoneMask("6827773569")
> MsgBox PhoneMask("682-777-3569")
>
> Function PhoneMask(ByVal sPhone)
> With CreateObject("VBScript.RegExp")
> .Global = True
> .Pattern = "([^0-9])" 'Pattern to remove all non-digits
> sPhone = .Replace(sPhone, Empty)
> .Pattern = "(\d{3})(\d{3})(\d{4})" 'Pattern to format the digits
> PhoneMask = .Replace(sPhone, "($1) $2-$3")
> End With
> End Function
> '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Interesting. What will result if the user inputs either more or fewer digits
than ten?

/Al


My System SpecsSystem Spec
Old 01-17-2009   #5 (permalink)
James Whitlow


 
 

Re: How do I create a mask for a phone #?

"Al Dunbar" <alandrub@xxxxxx> wrote in message
news:uDmi8pQeJHA.3856@xxxxxx
Quote:

>
> "James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
> news:%23NHw6vNeJHA.3776@xxxxxx
Quote:

>> "John Quinn" <JohnQuinn@xxxxxx> wrote in message
>> news:78FA7F5A-3D40-4307-8F83-C6E8D5E726B7@xxxxxx
Quote:

>>> Did this a while back, but I forgot how to do it again.
>>>
>>> I want to have the text box display (nnn)nnn-nnnn, either before or
>>> after
>>> the data is entered.
>>>
>>> bbb bbb bbbb or bbbbbbbbbb * b represents a blank or a space
>>>
>>> If we enter 682 777 3569 or 6827773569 or 682-777-3569 I want it to look
>>> like
>>>
>>> (682)777-3569.
>>>
>>> Appreciate the assistance.
>>
>> If you don't mind instantiating the 'VBScript.RegExp' object, you can
>> try the below code. It will handle the examples you gave, but not
>> international numbers.
>>
>> '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> MsgBox PhoneMask("682 777 3569")
>> MsgBox PhoneMask("6827773569")
>> MsgBox PhoneMask("682-777-3569")
>>
>> Function PhoneMask(ByVal sPhone)
>> With CreateObject("VBScript.RegExp")
>> .Global = True
>> .Pattern = "([^0-9])" 'Pattern to remove all non-digits
>> sPhone = .Replace(sPhone, Empty)
>> .Pattern = "(\d{3})(\d{3})(\d{4})" 'Pattern to format the digits
>> PhoneMask = .Replace(sPhone, "($1) $2-$3")
>> End With
>> End Function
>> '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Interesting. What will result if the user inputs either more or fewer
> digits than ten?
The above version will tack them onto them end if longer than 10 and only
remove non-digits with fewer than 10 (no formatting). Below is a small
revision that should handle a few more conditions.

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MsgBox PhoneMask("682 777 3569")
MsgBox PhoneMask("6827773569")
MsgBox PhoneMask("682-777-359")
MsgBox PhoneMask("6827")
MsgBox PhoneMask("US+01-682-777-3569")
MsgBox PhoneMask("US+01-6827773569x1234")
MsgBox PhoneMask("1-682-777-3569 Ext 1234")

Function PhoneMask(ByVal sPhone)
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "([^0-9])" 'Pattern to remove all non-digits
sPhone = .Replace(sPhone, Empty) & String(10, "?")
.Pattern = "[01]*([\d?]{1,3})([\d?]{1,3})([\d?]{1,4}).*" 'Format
PhoneMask = .Replace(sPhone, "($1) $2-$3")
End With
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





My System SpecsSystem Spec
Old 01-18-2009   #6 (permalink)
Dr J R Stockton


 
 

Re: How do I create a mask for a phone #?

In microsoft.public.scripting.vbscript message <ui5sNdNeJHA.5344@xxxxxx
NGP05.phx.gbl>, Sat, 17 Jan 2009 20:10:45, "Pegasus (MVP)"
<I.can@xxxxxx> posted:
Quote:

>
>"John Quinn" <JohnQuinn@xxxxxx> wrote in message
>news:78FA7F5A-3D40-4307-8F83-C6E8D5E726B7@xxxxxx
Quote:
Quote:

>> I want to have the text box display (nnn)nnn-nnnn, either before or after
>> the data is entered.
Quote:

>One way is to use the "replace" function to remove all spaces and dashes,
>followed by your own function to add the paretheses and the dash.
That should be possible too with replace.

But the OP needs to be aware that not all telephones in North America
necessarily have numbers which fit that ten-digit pattern.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Vista Phone Dialer can't dial phone rules correct Vista General
Vista Phone Dialer can't dial phone rules correct Vista General
Unable to create dial-up connections or enter phone properties Vista hardware & devices
To mask the email adress Vista mail
Where can I see my IP and net mask? Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46