# isNumeric()

Discussion in 'ASP General' started by Eugene Anthony, Jul 11, 2005.

1. ### Eugene AnthonyGuest

I have received the following feedback for the two functions bellow:

"The ISNUMERIC test is WORTHLESS for checking for an INT value, because
ISNUMERIC will happily accept DOUBLE values, such as 89.11998811777 and
other values that are simply *NOT* INT values."

<%
function isZip(input)

isZip = false

if len(input) = 5 then

isZip = (isNumeric(input))

end if

if len(input) = 10 then

z1 = left(input,5)
z2 = right(input,4)
z3 = mid(input,6,1)

isZip = (isNumeric(z1) and isNumeric(z2) and z3="-")

end if

end function

function isPhone(input)

Dim tempPh

tempPh = replace(input," ","")
tempPh = replace(input,"-","")
tempPh = replace(input,"-","")
tempPh = replace(input,"(","")
tempPh = replace(input,")","")

isPhone = (isNumeric(tempPh))

end function

%>

How do I solve the problem?.

Regards

Eugene Anthony

*** Sent via Developersdex http://www.developersdex.com ***

Eugene Anthony, Jul 11, 2005

2. ### Bob Barrows [MVP]Guest

If all you want to do is test whether it's an integer:

function isInt(pData)
on error resume next
dim test
test=clng(pData)
if err<>0 then
isInt=false
else
if pData <> test then
isInt=false
else
isInt=true
end if
end if
on error goto 0
end function

Eugene Anthony wrote:
> I have received the following feedback for the two functions bellow:
>
> "The ISNUMERIC test is WORTHLESS for checking for an INT value,
> because ISNUMERIC will happily accept DOUBLE values, such as
> 89.11998811777 and other values that are simply *NOT* INT values."
>
>
> <%
> function isZip(input)
>
> isZip = false
>
> if len(input) = 5 then
>
> isZip = (isNumeric(input))
>
> end if
>
> if len(input) = 10 then
>
> z1 = left(input,5)
> z2 = right(input,4)
> z3 = mid(input,6,1)
>
> isZip = (isNumeric(z1) and isNumeric(z2) and z3="-")
>
> end if
>
> end function
>
> function isPhone(input)
>
> Dim tempPh
>
> tempPh = replace(input," ","")
> tempPh = replace(input,"-","")
> tempPh = replace(input,"-","")
> tempPh = replace(input,"(","")
> tempPh = replace(input,")","")
>
> isPhone = (isNumeric(tempPh))
>
> end function
>
> %>
>
>
> How do I solve the problem?.
>
> Your help is kindly appreciated.
>
> Regards
>
> Eugene Anthony
>
> *** Sent via Developersdex http://www.developersdex.com ***

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Bob Barrows [MVP], Jul 11, 2005

3. ### Eugene AnthonyGuest

When I did this I am getting an invalid phone and zip code.

function isInt(input)

on error resume next

Dim Tem
Tem = Clng(input)

if err <> 0 then

isInt = false

else

if(Tem <> input) then
isInt = false
else
isInt = true
end if

end if

on error goto 0

end function

function isZip(input)

isZip = false

if len(input) = 5 then

isZip = (isInt(input))

end if

if len(input) = 10 then

z1 = left(input,5)
z2 = right(input,4)
z3 = mid(input,6,1)

isZip = (isInt(z1) and isInt(z2) and z3="-")

end if

end function

function isPhone(input)

Dim tempPh

tempPh = replace(input," ","")
tempPh = replace(input,"-","")
tempPh = replace(input,"-","")
tempPh = replace(input,"(","")
tempPh = replace(input,")","")

isPhone = (isInt(tempPh))

end function

Regards

Eugene Anthony

*** Sent via Developersdex http://www.developersdex.com ***

Eugene Anthony, Jul 11, 2005
4. ### Roland HallGuest

"Eugene Anthony" wrote in message
news:O0R4u%...
: When I did this I am getting an invalid phone and zip code.
:
:
: function isInt(input)
:
: on error resume next
:
: Dim Tem
: Tem = Clng(input)
:
: if err <> 0 then
:
: isInt = false
:
: else
:
: if(Tem <> input) then
: isInt = false
: else
: isInt = true
: end if
:
: end if
:
: on error goto 0
:
: end function
:
: function isZip(input)
:
: isZip = false
:
: if len(input) = 5 then
:
: isZip = (isInt(input))
:
: end if
:
: if len(input) = 10 then
:
: z1 = left(input,5)
: z2 = right(input,4)
: z3 = mid(input,6,1)
:
: isZip = (isInt(z1) and isInt(z2) and z3="-")
:
: end if
:
: end function
:
: function isPhone(input)
:
: Dim tempPh
:
: tempPh = replace(input," ","")
: tempPh = replace(input,"-","")
: tempPh = replace(input,"-","")
: tempPh = replace(input,"(","")
: tempPh = replace(input,")","")
:
: isPhone = (isInt(tempPh))
:
: end function

It's probably easier using a regular expression and just have the user enter
just digits.

Ex.

<%@ Language=VBScript %>
<%
Option Explicit
Response.Buffer = True

function checkPhone(strInput)
dim re, matches, match
set re = new RegExp
re.Pattern = "\d{10}" ' phone 10 digits decimal only
set match = re.Execute(strInput)
if match.count > 0 then
checkPhone = true
else
checkPhone = false
end if
set match = nothing
set re = nothing
end function

Response.Write "9009766969: " & checkPhone("9009766969") & "<br />" & vbCrLf
Response.Write "800XYZ1234: " & checkPhone("800XYZ1234") & "<br />" & vbCrLf
%>

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
MSDN Library - http://msdn.microsoft.com/library/default.asp

Roland Hall, Jul 11, 2005
5. ### Bob Barrows [MVP]Guest

Roland Hall wrote:
>
> It's probably easier using a regular expression and just have the
> user enter just digits.
>
> Ex.
>
> <%@ Language=VBScript %>
> <%
> Option Explicit
> Response.Buffer = True
>
> function checkPhone(strInput)
> dim re, matches, match
> set re = new RegExp
> re.Pattern = "\d{10}" ' phone 10 digits decimal only
> set match = re.Execute(strInput)
> if match.count > 0 then
> checkPhone = true
> else
> checkPhone = false
> end if
> set match = nothing
> set re = nothing
> end function
>
> Response.Write "9009766969: " & checkPhone("9009766969") & "<br />" &
> vbCrLf Response.Write "800XYZ1234: " & checkPhone("800XYZ1234") &
> "<br />" & vbCrLf %>
>

And, from http://regexlib.com/REDetails.aspx?regexp_id=924 here's the zip
code pattern:

re.Pattern = "^\d{5}((-|\s)?\d{4})?\$"Bob Barrows

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Bob Barrows [MVP], Jul 11, 2005