Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help, Please! 4

Status
Not open for further replies.

NewGuy

Programmer
Dec 28, 2000
31
0
0
CA
I have tried to search Google and Search this site with no luck.

My problem is, I have a very simple data-entry form with a date and a textbox to enter a telephone number into.

I want to do tests to ensure a number is entered and not text like "hi there".

I was thinking "REGEXP" but it keeps crashing so I must not be doing something right.

Any suggestions for ensuring that only a phone number is entered into the TEXTBOX would be appreciate.

Thank you.....
 
Specifically a phone number or restrict to numbers. For restriction to numbers you can use something like this:
Code:
Private Sub txtCPhone_KeyPress(KeyAscii As Integer)
    If (KeyAscii > vbKey9 Or KeyAscii < vbKey0) And KeyAscii <> vbKeyBack And KeyAscii <> 45 Then KeyAscii = 0
End Sub

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Sorry to be picky but, the input is a Telephone Number specifically. So the best thing is probably for me to "SOMEHOW" test it after the user does the entry.
 
You will need to validate the text box in conjuction with limiting the input to numbers. Something like this:

Code:
Private Sub Text1_Validate(Cancel As Boolean)
    Select Case Len(Text1.Text)
        Case 7
            Text1.Text = Format$(Text1.Text, "###-####")
        Case 10
            Text1.Text = Format$(Text1.Text, "(###) ###-####")
        Case Else
            MsgBox "Invalid Phone Number"
            Cancel = True
    End Select
End Sub



If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Thanks for the idea. But to be honest, not what I was hoping for.
 
Use Javascript?

in Perl the regex would read like this:-

#!/usr/bin/perl

$_ = <STDIN>;

chomp $_;

print "illegal" if $_ !~ m/^[0-9]+$/;



Kind Regards
Duncan
 
I had just started looking at other controls beside a textbox. Mask Edit Control looks like what I need.

If not for a bad headache all morning, I would have thought of that sooner.
 
And... if you want a regexp solution for VB then this is what I've used in the past:

Code:
Option Explicit

Private Sub Form_Load()
    MsgBox ValidPhoneNumber("(319) 385-4742")  '& vbCrLf & "385-4742" & vbCrLf & "319.385.4742" & vbCrLf & "319-385-4742"
End Sub

Private Function ValidPhoneNumber(PhoneNumber As String) As Boolean
    With New RegExp
        .MultiLine = True
        .Pattern = "^((\([0-9]{3})\) ([0-9]{3})-([0-9]{4}))|^(([0-9]{3})-([0-9]{4}))|^(([0-9]{3})-([0-9]{3})-([0-9]{4}))"
        .IgnoreCase = True
        .Global = True
        ValidPhoneNumber = .Test(PhoneNumber)
    End With
End Function

This will accept the following formats:

(555) 555-5555
555-555-5555
555-5555

If you need to remove one, look for the pipe(|) symbol and remove the patterns accordingly.
 
You need to reference "Microsoft VBScript Regular Expressions x.x" in your project. On my machine I have version 5.5. Checkout the project from sourcesafe, if applicable, and create a reference and then check it back in.

Here's the code:

if PhoneNumber(txtPhone.text) then
do_this
else
do_that
end if

Function PhoneNumber(strng As String) As Boolean
Dim regEx, Match, Matches
Dim retstr As String ' Create variable.
Set regEx = New RegExp ' Create a regular expression.

PhoneNumber = False
regEx.Pattern = "((\(\d{3}\)?)|(\d{3}-))?\d{3}-\d{4}" ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Set Matches = regEx.Execute(strng) ' Execute search.
For Each Match In Matches ' Iterate Matches collection.
PhoneNumber = True
Next
End Function

HTH


ciao for niao!

AMACycle

American Motorcyclist Association
 
Thank you to both BJD4JC and AMACYCLELOONY.

The code from BJD4JC works perfectly and allows me to have the program work the way I want it to.

Very, Very Much EXTREMELY Appreciated.

This thread is now CLOSED!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top