Check for positive integer in form field

4 replies
Hi,

I'm making a simple order page in classic asp.
In the field where quantity of the product is typed in, I need to make sure that it's a posive integer (with no '.'s or ','s).
Is there an easy way to check that, either in classic asp or javascript?

Thanks.

Best regards,
Thomas
#check #field #form #integer #positive
  • Profile picture of the author ThomasTe
    I've tried this, but it seems to return 0 nomatter what I input...

    <%
    NumberOfLicenses=100.2
    ValidNumberCheck=0
    If NumberOfLicenses = "" Then
    ValidNumberCheck=1
    End If
    If Not IsNumeric(ValidNumberCheck) Then
    ValidNumberCheck=1
    end if
    if Instr(strPrice, ".") > 0 Then
    ValidNumberCheck=1
    end if
    if Instr(strPrice, ",") > 0 Then
    ValidNumberCheck=1
    end if
    Response.write(ValidNumberCheck&"-"&NumberOfLicenses)
    %>
    {{ DiscussionBoard.errors[3534364].message }}
    • Profile picture of the author jb5665
      Are you checking that strPrice or NumberOfLicenses contains the "." or ","? I don't quite get which one you are checking?

      You could also look at using int.TryParse() to convert to an integer rather than IsNumeric.

      Good luck!

      James
      {{ DiscussionBoard.errors[3534885].message }}
  • U should use a combination of javascript and server side, but serverside is mandatory.
    I dont know that language, but why not use comparisons?

    if NumberOfLicenses > 0 Valid

    The greater than comparison SHOULD automatically check to see if it is a valid integer and the zero should confirm that it is not a negative integer so that kills 2 birds in one stone.

    Instead of checking for the decimal point, you can, in php, floor the integer, or round it up, instead of throwing an error.

    If Not IsNumeric(ValidNumberCheck) Then
    ValidNumberCheck=1

    Prob just me, but not is , I just dont like. I would do it like this ...

    IF isNuemeric(ValidNumberCheck) Than nothing
    ELSE ValidNumberCheck=1

    But thats prob just me, like i said
    {{ DiscussionBoard.errors[3537931].message }}
  • Profile picture of the author ThomasTe
    Thanks for the help. I just discovered that one of my variables should have been named 'NumberOfLicenses' and not 'StrPrice'.
    However, this did not do the job. It still accepted letters.
    So I included the line for isNumeric, and also checked for '+' and '-', and now it works.

    Thanks, guys.

    Best regards,
    Thomas
    {{ DiscussionBoard.errors[3540259].message }}

Trending Topics