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

I have this sub routine that valida

Status
Not open for further replies.

matrixxx

MIS
Oct 30, 2001
12
US
I have this sub routine that validates a phone number. Everything works(i think) until i get to the instr. The purpose of the instr is to flag an error if the user puts a "-" in the string. Now I have two questions. One if I am checking to see if it isnumeric, shouldn't the "-" produce a flag. Secondly, why doesn't the instr flag the "-", its not a special character of some sort, is it.

fvalue=123-444-23

Sub Check_Phone(fvalue,dname)
If len(Trim(fvalue))=0 Then
ErrCount=ErrCount+1
AddErr(Cstr(ErrCount) + ". "+ dname+ " cannot be empty")
else

If IsNumeric(Trim(fvalue))=False Then
ErrCount=ErrCount+1
AddErr(CStr(ErrCount) + ". " + dname + " is not a valid Number")
else

if len(CStr(fvalue)) < 10 then
ErrCount=Errcount+1
AddErr(CStr(ErrCount) + &quot;. &quot; + dname + &quot;doesn't appear to be a valid phone no.&quot;)

If instr(1, fvalue, &quot;-&quot;) <> 0 then
ErrCount=Errcount+1
AddErr(CStr(ErrCount) + &quot;. &quot; + dname + &quot;phone number doesn't need to hyphenated&quot;)
End If
End IF
End If
End If
End Sub
 
Try:

Code:
fvalue = &quot;123-444-23&quot;

I think at the moment it is treating that as a calculation so in effect you are ending up with fvalue = -344.

--James
 
Change the instring function as below:

INSTR(fvalue,&quot;-&quot;)

Then it will return the number of times &quot;-&quot; apprears in the string. I think, this will solve the problem.

Shihab
 
I changed instr to convert fvalue to a string, no go.

If instr(CStr(fvalue, &quot;-&quot;)) <> 0 then
ErrCount=Errcount+1
AddErr(CStr(ErrCount) + &quot;. &quot; + dname + &quot;phone number doesn't need to hyphenated&quot;)
End If

I changed instr to:

If instr(fvalue, &quot;-&quot;) <> 0 then
ErrCount=Errcount+1
AddErr(CStr(ErrCount) + &quot;. &quot; + dname
+ &quot;phone number doesn't need to hyphenated&quot;)
End If

I must be missing something simple ;(
 
You don't need to use the CStr, just make sure that you are passing a string rather than an number into the function:

Code:
Sub Check_Phone(fvalue,dname)
  ...
End Sub

Dim strTest
strTest = &quot;123-444-23&quot;

Check_Phone(strTest, &quot;Phone&quot;)

--James
 

Got it working. To make it work i had to move the if statement before checking for isnumeric.

Sub Check_Phone(fvalue,dname)
If len(Trim(fvalue))=0 Then
ErrCount=ErrCount+1
AddErr(Cstr(ErrCount) + &quot;. &quot;+ dname+ &quot; cannot be empty&quot;)
else

If instr(fvalue, &quot;-&quot;) <> 0 then
ErrCount=Errcount+1
AddErr(CStr(ErrCount) + &quot;. &quot; + dname + &quot;phone number doesn't need to hyphenated&quot;)
else

If IsNumeric(Trim(fvalue))=False Then
ErrCount=ErrCount+1
AddErr(CStr(ErrCount) + &quot;. &quot; + dname + &quot; is not a valid Number&quot;)
else

if len(CStr(fvalue)) < 10 then
ErrCount=Errcount+1
AddErr(CStr(ErrCount) + &quot;. &quot; + dname + &quot;doesn't appear to be a valid phone no.&quot;)

End If
End IF
End If
End If
End Sub

thanks for all your help
 
CStr the phone number first, and secondly, check the pgone number for the &quot;-&quot; before checking it's numeric validity

another option is a function i use QUITE often :

use this as your checkvalues :

Checkvalues = &quot;(|)| |-|\|/|.&quot; ' these are for people that use standard phone number extra chars and put periods dashes, spaces any of that...

If ChkArrayInstr(CheckValues, PhoneNumber,&quot;|&quot;) Then
Response.write &quot;BadNumber&quot;
Else
....continue code





Function ChkArrayInstr(Values,Value,Delim)
If ISArray(Values) Then
ChkArrayArr = Values
Else
ChkArrayArr = Split(Values,Delim)
End If
ChkArrayInstr = False
For ChkArrayArrCounter=0 to Ubound(ChkArrayArr)
If Instr(1,Value,ChkArrayArr(ChkArrayArrCounter),vbTextCompare) > 0 Then
ChkArrayInstr = True
End If
Next
End Function

 
lol looks like we were posting to this at the same time matrixx :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top