facing couple problems using my function.

Rani

Well-known member
Joined
Nov 19, 2005
Messages
71
Programming Experience
Beginner
1.
I have a function below it works with out extn. if the phone number is with the extn when i use isnumeric. if you look at my function below, i am replacing extn with x. when i use isnumeric, because of x included (508-614-3344 x 456)
in the number i can't get this right. how would you suggest i do this?

2.
Also, if i were to call this function for another phone validation
this part of the lines i have problems:
VB.NET:
[/SIZE]
[SIZE=2][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].valhphone.IsValid = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000][COLOR=navy]Me.valhphone.Text = "Please enter a valid telephone number"[/COLOR][/COLOR][/SIZE]

[SIZE=2][COLOR=#008000][COLOR=#000080]instead of requiredfieldvalidator, using label would be the right way to go? please advise. thanks.[/COLOR][/COLOR][/SIZE][COLOR=#008000]
[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]*********************[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] phone_validate([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] phone [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Const[/COLOR][/SIZE][SIZE=2] defaultareacode = "508"[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strextras [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] intResult [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strext [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'remove all special characters first[/COLOR][/SIZE]
[SIZE=2]strextras = Replace(phone, "(", "")[/SIZE]
[SIZE=2]strextras = Replace(strextras, ")", "")[/SIZE]
[SIZE=2]strextras = Replace(strextras, "-", "")[/SIZE]
[SIZE=2]strextras = Replace(strextras, " ", "")[/SIZE]
[SIZE=2]strextras = Replace(strextras, "extn", "x")[/SIZE]
[SIZE=2][COLOR=#008000]'if this is a number[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IsNumeric(strextras) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'break up the digits into a number and extension if any[/COLOR][/SIZE]
[SIZE=2]intResult = InStr(1, strextras, "x", vbTextCompare)[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] intResult > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]strext = Mid(strextras, intResult + 1)[/SIZE]
[SIZE=2]phone = Left(strextras, intResult - 1)[/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2]phone = strextras[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'remove the prefix 1 if there is one[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Left(phone, 1) = "1" [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]phone = Mid(phone, 2)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'After prefix is removed it it is 7 digits include the default areacode[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Len(phone) <> 7 [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] Len(phone) <> 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].valhphone.IsValid = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000][COLOR=navy]Me.valhphone.Text = "Please enter a valid telephone number"[/COLOR][/COLOR][/SIZE][COLOR=#008000]
[/COLOR][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Len(phone) = 7 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]phone = defaultareacode & phone[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].valhphone.IsValid = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'build new number[/COLOR][/SIZE]
[SIZE=2]phone = "(" & Left(phone, 3) & ")" _[/SIZE]
[SIZE=2]& Mid(phone, 4, 3) & "-" _[/SIZE]
[SIZE=2]& Right(phone, 4)[/SIZE]
[SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Len(phone) = 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].valhphone.IsValid = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]phone = "(" & Left(phone, 3) & ")" _[/SIZE]
[SIZE=2]& Mid(phone, 4, 3) & "-" _[/SIZE]
[SIZE=2]& Right(phone, 4)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].valhphone.IsValid = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].valhphone.Text = "please enter an integer number"[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] phone[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[/COLOR][/SIZE]
 
Last edited by a moderator:
Back
Top