1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I check if some user input is an integer?

Discussion in 'C#' started by dylanj, Apr 4, 2008.

  1. #1
    Hi there

    I'm making a CMS, and each page is referenced by ID. For instance, a URL might be:
    www.mydomain.com/index.asp?p=142
    Code (markup):
    Now, it is possible to type in:
    www.mydomain.com/index.asp?p=abc
    Code (markup):
    and you get a big ugly error because the ID field in my database is an autonumber, so there are no delimiters, and the field doesn't accept text. Anyone know how to check if Request.Querystring("p") is an integer?

    Thanks in advance

    Dylan
     
    dylanj, Apr 4, 2008 IP
  2. irock2

    irock2 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this if you use VB.Net, else convert to C#:

    Dim theString As String = Request.Querystring("p")

    If IsNumeric(theString) Then
    Do Something
    Else
    Do Something Else
    End If
     
    irock2, Apr 4, 2008 IP
  3. dylanj

    dylanj Peon

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Thanks, that fixed it :)
     
    dylanj, Apr 4, 2008 IP
  4. InfoSmith

    InfoSmith Peon

    Messages:
    884
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    IsNumeric(123.45) = True

    if you really careful, check Int(vInt) = vInt
     
    InfoSmith, Apr 6, 2008 IP
  5. TheCashCow25

    TheCashCow25 Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I know you already got an answer, however if you want to stick to classic ASP, the following should work:

    theVariable = Request.Querystring("p")
    If theVariable + 1 > 0 Then
    ' This is a number
    Else
    ' It would have returned -1, and we know that it's not a number
    End If
     
    TheCashCow25, Apr 8, 2008 IP
  6. dylanj

    dylanj Peon

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #6
    The IsNumeric() function is in classic ASP
     
    dylanj, Apr 14, 2008 IP
  7. salman4raza

    salman4raza Peon

    Messages:
    373
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    IsNumeric(request("input"))

    this will return true of false.
     
    salman4raza, Apr 24, 2008 IP
  8. dylanj

    dylanj Peon

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #8
    Yup, that's what I wanted
     
    dylanj, Apr 28, 2008 IP