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?
ervinlohAsked:
Who is Participating?
 
Ravi97Connect With a Mentor 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
 
robbertCommented:
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
 
robbertCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
robbertCommented:
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
 
hobsterCommented:
If VarType(var_qty) = vbInteger Then
   Response.Write "string is an integer"
Else
   Response.Write "not an integer"
End If
0
 
jamestthomasCommented:
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
 
stefanxCommented:
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
 
Yury_DelendikCommented:
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
 
ervinlohAuthor 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
All Courses

From novice to tech pro — start learning today.