# integer validation

Does anyone knows how to validate whether a string is of type integer of not?

I have a form which collects a part quantity. This quantity must be of type integer. As such, I cannot use the IsNumeric function.

Any ideas?
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Tinkering :-)

(TypeOf(myVar) should get it right, but generates an error.)

On Error Resume Next
If CInt(myVar) = CDbl(myVar) Then
blnInteger = True
myVar = CInt(myVar)
End If
If Err.Number <> 0 Then
blnInteger = False
End If
On Error GoTo 0

If blnInteger Then
'save myVar as Integer
End If
0
Commented:
Or, just more tinkering :-)

myVar = "1.23"
strRef = "0123456789"

blnInteger = True
For i = 1 To Len(myVar)
If Not Instr(1, strRef, Mid(CStr(myVar),1)) > 0 Then
blnInteger = False
End If
Next
0
Commented:
To be perfect :-)

myVar = "1.23"
myVar = CStr(myVar)
strRef = "0123456789"

blnInteger = True
For i = 1 To Len(myVar)
If Not Instr(1, strRef, Mid(myVar, 1)) > 0 Then
blnInteger = False
End If
Next
0
Commented:
If VarType(var_qty) = vbInteger Then
Response.Write "string is an integer"
Else
Response.Write "not an integer"
End If
0
Commented:
here is  a javascript solution:

//####################################################################
function NumericsOnly(varvalue, someObject)
{
var valid="01234567890";
for(x=0;x < varvalue.length; ++x)
{

if (valid.indexOf(varvalue.charAt(x),0)==-1)
{
if(someObject)
{
alert("Please enter numeric data only.");
someObject.focus();
}
return(false);
}
}
return(true);
}
0
Commented:
Or just simply like this :

Dim iVal
Dim Tmp

On Error Resume Next

Tmp = Request.Form("theinputvalue")
If Not IsNumeric(Tmp) Then
Response.Write "Not Numeric"
Response.End
End If
If Val(Tmp) < -32768 Or Val(Tmp) > 32767 Then
Response.Write "Not an Integer"
Response.End
End If
iVal = CInt(Tmp)
Response.Write "Whoopee - it is an integer of value " & iVal

0
Commented:
use this function check whether the entered value is integer
the sent value should be trimmed

function isnumeric(strvalue)
{
var strlen = strvalue.length;
var i;
for ( i = 0; i < strlen ; ++i )
{
if (!((strvalue.charAt(i) >= '0') && (strvalue.charAt(i) <= '9')))
{
return false;
}
}
return true;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Simply?

VBScript:
<%
...

Set regEx = New RegExp
regEx.Pattern = "^(\-|\+)?\d+(\.\d*)?\$" ' for float numbers
' regEx.Pattern = "^(\-|\+)?\d+\$" ' for integer
' regEx.Pattern = "^\d+\$" ' for non negative, etc.
regEx.IgnoreCase = True

If regEx.Test(val) Then
Response.Write "Number"
Else
Response.Write "Not a number"
Else
%>

JavaScript:
<%
...

pattern = /^(\-|\+)?\d+(\.\d*)?\$/;
// pattern = /^(\-|\+)?\d+\$/;

if(pattern.test(val)) ...
%>

It's nice, isn't it? :-)
0
Author Commented:
Thank you for all the responses.

To: hobster

If VarType(var_qty) = vbInteger Then
Response.Write "string is an integer"
Else
Response.Write "not an integer"
End If

does not work. Texts read from a textbox will be always recognized as string. As such, it won't work.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.