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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stripping invalid characters 1

Status
Not open for further replies.

paulminne

Programmer
Joined
Nov 27, 2002
Messages
80
Location
AU
Hi,

I was wondering if anyone could give me some advice on how to run a function which will validate a phone number field and check for any invalid characters (eg. letters). Excluding ()+ -

Thanks,

- Paul
 
One of the ways that I have done this is to strip the phone number to numeric values only then format it in the way that you want

e.g something like:
[tt]
Public function FormatPhoneNum(strNumber as string) as string

dim lng as Long
dim strTemp as string

strtemp = vbNullString

for lng = 1 to len(trim$(strnumber))
If IsNumeric(Mid(strNumber, i, 1)) Then
strTemp = strTemp & Mid(strNumber, i, 1)
end if
next lng

FormatPhoneNum= Format$(strTemp, "(@@) @@@ @@@@")

exit function
[/tt]

Cheers,
Dan
 
Thanks for your help!

I figured out another way using the Ascii values on the keypress event. The only thing I need to do now is to store the contents of the text before before it checks. Because it does the check fine, but still allows you to place the value into the box after the message box appears.

My code:

Private Sub txtPhone_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case 8
'Allows backspace and delete characters
Case 32
'Allows space
'Case 40 To 41
'Allows opening and closing parenthisis
Case 45
'Allows hyphen
Case 48 To 57
'0-9 --> OK
Case Else
'MsgBox "Invalid character", vbExclamation
End Select

End Sub

Cheers,

Paul
 
I do something similar to restrict input into numeric fields. I turn invalid characters to Null. e.g.
[tt]
Private Sub txtEndYear_KeyPress(KeyAscii As Integer)
Const ASCII_BACKSPACE As Long = 8
Const ASCII_NULL As Long = 1

If KeyAscii = ASCII_BACKSPACE Then Exit Sub
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = ASCII_NULL

End Sub
[/tt]

Just add "KeyAscii = ASCII_NULL" for values that are invalid.

I usually use this method when I want to avoid a message box occuring and how you want to interact with the user.

However, to be complete you're need an After_update method as well because you may get extra spaces e.g (exagerated example)
[tt]
(01) 111 1111 versus (01) 111 1111
[/tt]
I guess it depends on how neatly (business rules) you want to store your phone numbers

Cheers,
Dan
 
opps, i added to the wrong sentence...

"I usually use this method when I want to avoid a message box occuring."

"I guess it depends on how neatly (business rules) you want to store your phone numbers and how you want to interact with the user."
 
Hi,

I understand what you mean with the Null characters, but how will I achieve this in my code? I am unsure as to how I should go about it?

Thanks,

- Paul
 
Alright, sorry to be a pain. But I have tried this code you gave me and liked it, the:

If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = ASCII_NULL
End If

However, can I now allow other characters to be accepted?

Thanks again.

- Paul
 
sorry, it has been a loooong day...I know what you meant now. Its all sorted.

Thanks again for your help.

- Paul
 
If you always use the same format for entering the phone number, you can avoid a lot of that code and use the built-in Like comparison, such as:

PhoneNumber Like "###-###-####" Or PhoneNumber Like "(###)###-####"

You can probably also work the Like comparison even further to weed out any unwanted characters, such as this would find if the string has any letters in it:

PhoneNumber Like "*[a-Z]*"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top