Phone Number Validation

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
I have been working on a script to check if a user have entered a valid phone number into an InputBox. If they enter invalid information then it should keep looping until they enter valid information. Right now the following script only loops twice and lets invalid information pass through.

Any suggestions on how to fix this?

Code:
PNumber = Application.InputBox("Please enter the best phone number to contact you at", "Required", Type:=2)
    
    On Error GoTo ErrorHandler
    If PNumber Like "###-###-####" Then
    MsgBox "Stop"
    Else
ErrorHandler:
    PNumber = Application.InputBox("You didn't enter your phone number correctly" & vbNewLine _
    & "Please re-enter the best phone number to contact you at" & vbNewLine & vbNewLine _
    & "The correct format is ###-###-####", "Required", PNumber, Type:=2)
    End If
Resume Next
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
why can't you just use Data Validation - Text Length - Equal to... 10. And then format the cell ###-###-####

?
 
Upvote 0
Maybe ...
Code:
    Dim sPhon       As String
 
    sPhon = InputBox(Prompt:="Please enter the best phone number to contact you", _
                     Title:="Required")
    If Len(sPhon) = 0 Then Exit Sub  
    sPhon = Replace(Replace(Replace(Replace(sPhon, "(", ""), ")", ""), "-", ""), " ", "")
 
    Do While Not IsNumeric(sPhon) Or Len(sPhon) <> 10
        sPhon = InputBox(Prompt:="Invalid, try again", _
                         Title:="Oops!")
        If Len(sPhon) = 0 Then Exit Sub 
        sPhon = Replace(Replace(Replace(Replace(sPhon, "(", ""), ")", ""), "-", ""), " ", "")
    Loop
 
    MsgBox "Good job!"
    ' carry on
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top