locked
validate integer value RRS feed

  • Question

  • User-984849328 posted

     Hey,

    simple when you know how I'm sure, but I dont know how to validate a value as an integer, heres my code:

     

    dim myInt as integer
    dim this as boolean
    
    if myInt is integer then
    
       this = true
    
    else
    
       this = false
    
    end if
    I dont know what to put in the 1st if line, ive seen some val(myInt) stuff, and tryparse integer but not sure how to get it to work, thanks
     
      
    Tuesday, February 3, 2009 11:51 AM

Answers

  • User1512755243 posted

    Hi

      You can do somethin like below....

     

    Dim ts As Integer = 45.56

    Dim str As String

    str = CType(ts, String)

    If System.Text.RegularExpressions.Regex.IsMatch(str, "\d") Then

    this = true

    End If

    I hope it works for u.

    Thanks,

    Kumar Pindiprolu. 

     

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2009 12:31 PM

All replies

  • User-1995538749 posted

    Use the Int32.TryParse method something like this: 

    Dim s As String = "-1"
    Dim result As Integer = 0
    
    If Int32.TryParse(s, result) Then
    	Response.Write("Is Integer")
    Else
    	Response.Write("Is Not Integer")
    End If
    Tuesday, February 3, 2009 12:01 PM
  • User-984849328 posted

     this is the part i dont understand, why are two values being compared?

    e.g. user enters value into text box, assign that to 'myInteger'

    that variable could be a float or even an alphabetic character... im not understanding the process of -1 etc

     

    sorry im just not getting my head round things today! lol

    Tuesday, February 3, 2009 12:11 PM
  • User-1995538749 posted

    If you want to check the value within your TextBox, then do something like this instead: 

    Dim result As Integer = 0
    
    If Int32.TryParse(TextBox1.Text, result) Then
    	Response.Write("Is Integer")
    Else
    	Response.Write("Is Not Integer")
    End If
    Tuesday, February 3, 2009 12:20 PM
  • User1512755243 posted

    Hi

      You can do somethin like below....

     

    Dim ts As Integer = 45.56

    Dim str As String

    str = CType(ts, String)

    If System.Text.RegularExpressions.Regex.IsMatch(str, "\d") Then

    this = true

    End If

    I hope it works for u.

    Thanks,

    Kumar Pindiprolu. 

     

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2009 12:31 PM