Web Analytics Made Easy -
StatCounter making sure a variable is a number - CodingForum

Announcement

Collapse
No announcement yet.

making sure a variable is a number

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • making sure a variable is a number

    i am trying to make sure that a variable is a number, before i update the database with it (the page just gets an error at the moment, which is ok but i would prefer if it jsut redirected back to the form
    My tech/code blog

  • #2
    do you mean like making sure the variable contains a valid number? such as 1423.5675? instead of 1234.223.45

    or is it something else you're after like making sure the variable is holding the integer 11 instead of the string "11"?
    anthony

    Comment


    • #3
      ASP 3 or ASP NET

      If Not IsNumeric(Value) Then
      ' Return False
      End If


      If IsNumeric(Value) Then
      ' Return True
      End If

      Comment


      • #4
        Remember that in VBScript the variables are of the datatype of variant. So if you want to ensure that the value of the variable is an integar and not a string, use something like the following


        If Not IsNumeric(someVariable) Then CInt(someVariable)

        Comment


        • #5
          if it is not numeric you cannot Cint :-))

          Comment


          • #6
            ok true The point I was trying to make is that because with vbscript you can not declare the variables datatype when you declare the variable like you do in vb like below

            vb example - Dim intThisVariable as Integar
            vbscript example - Dim intThisVariable

            Therefore if you are debugging and do a response.Write intThisVariable, you may see 123 but this does not mean it is an integar at all. It very well could be a string. If you are trying to do an update on a database with the datafield set to be an integar or a number datatype and you get an error then use CInt to ensure that this variable is indeed an integar.

            Comment


            • #7
              then you could do

              If Not IsNumeric(Value) Then
              var = 0
              'or Var = NULL
              Else
              var = Value
              End If

              Comment


              • #8
                Question for you castali, since we can not know the value of the variable, assigning a specific value such as var = 0 in my case would not work. So what is wrong with using CInt to ensure that it was being read as an integar? I ask because I used this to solve a SQL query I wrote a few years ago where the value to human eyes was a number but it was actually a string and not an integar and that was causing the SQL query to generate an error. My query looked something like

                sSQL = "SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = & " intID & ""

                when the response.write sSQL was done it generated
                SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = 1234 yet there was an error so all I had to do was CInt(intID) and it worked like a charm.

                Comment


                • #9
                  really strange

                  there is no difference between Cint(20) and 20 .. for ASP3
                  ... it is not the same for VB NET

                  and if you do a response.write sSQL after the Cint you wil get the same than before .... or ?

                  If Not IsNumeric(intID) Then
                  Exit Sub
                  Else
                  sSQL = "SELECT coid,coName,address,city,state,phone FROM tableA WHERE coid = "& intID &" ;"
                  ' ....do the job
                  End If

                  should work

                  Comment


                  • #10
                    That was what I thought before hand, and I found out differently. It was because the value of intID was pulled from other databases as well as other tables from within this particular database, and sometimes the datatype of the column was text (string). Therefore when I ran the SQL query it was seeing it as a string and not as an integar so I just used CInt(intID) to ensure that the query read the value as an integar. Also sometimes the value may have been stripped from inside a longer string and then CInt was needed to make sure it was read as an integar

                    Comment


                    • #11
                      good to know .. i had never such a problem

                      thanks for the info !

                      Comment


                      • #12
                        Let me ask you something else... do you mean number, as in:

                        1.234

                        +12,345

                        or are you looking for just DIGITS (for instance, an integer)?

                        If you want to check your data for that, there's a more reliable way, using regular expressions:

                        Code:
                        <%
                        Function IsDigits(str)
                        	Dim objRegEx
                        	Set objRegEx = New RegExp
                        	objRegEx.Pattern = "^\d+$"
                        	IsDigits = objRegEx.Test(str)
                        	Set objRegEx = Nothing
                        End Function
                        
                        Response.Write(IsDigits("1.35")) 'Will produce false
                        %>
                        Former ASP Forum Moderator - I'm back!

                        If you can teach yourself how to learn, you can learn anything. ;)

                        Comment


                        • #13
                          I agree with castali

                          Originally posted by miranda
                          My query looked something like

                          sSQL = "SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = & " intID & ""

                          when the response.write sSQL was done it generated
                          SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = 1234 yet there was an error so all I had to do was CInt(intID) and it worked like a charm.
                          This would work even if the passed coid is stored in a vbscript string variable.

                          strID = "123"
                          sSQL = "SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = " & strID

                          You don't have to CInt it, just don't put single quotes around the coid value like below, so that SQL will treat it as a number and not a string.
                          strID = "123"
                          sSQL = "SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = '" & strID & "'"

                          I'm puzzled why this didn't work for you

                          SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = 1234

                          Doing a CInt or not in the vbscript variable that holds the value 1234 produces the same SQL statement as what castali earlier mentioned.
                          Glenn
                          vBulletin Mods That Rock!

                          Comment


                          • #14
                            yes I am a bit surprised but .....

                            Comment

                            Working...
                            X