Jump to content

If IsNumber($Num) Then


totoymola
 Share

Recommended Posts

Hi. This is my first post ;)

I'm having a problem with my script. I want to make an inputbox that will check if the input is a number. I spent several hours to make this work, and I finally figured out a solution. However, it is NOT perfect.. This is my current working script so far.

#CS#=#=#=#=#=#=#=#=#=#=#=#=#=#=#

    PURPOSE : NUMBER CHECKER

#CE#=#=#=#=#=#=#=#=#=#=#=#=#=#=#


While 1

$Num = InputBox("Number Checker", "Please enter a VALID number greater than or equal to zero.", "0")

If @error = 0 Then

    If $Num >= 1 Or $Num = "0" Then
        MsgBox(0, "Result", "VALID")
    Else
        MsgBox(0, "Result", "NOT VALID")
    EndIf

Else
    Exit
    
EndIf

WEnd

Before this, I tried using If $Num >= 0 Then instead of If $Num >= 1 Or $Num = "0" Then, but it didn't work. It always return "VALID".

Then I thought of using the IsNumber function, and it didn't work too. It always returns "NOT VALID" Here is the code with the IsNumber:

#CS#=#=#=#=#=#=#=#=#=#=#=#=#=#=#

    PURPOSE : NUMBER CHECKER

#CE#=#=#=#=#=#=#=#=#=#=#=#=#=#=#


While 1

$Num = InputBox("Number Checker", "Please enter a VALID number greater than or equal to zero.", "0")

If @error = 0 Then

    If IsNumber($Num) Then
        MsgBox(0, "Result", "VALID")
    Else
        MsgBox(0, "Result", "NOT VALID")
    EndIf

Else
    Exit
    
EndIf

WEnd

Please tell me what is wrong with this.

Thanks! :P

Edited by totoymola
Link to comment
Share on other sites

$Num = InputBox("Number Checker", "Please enter a VALID number greater than or equal to zero.","0")
If $Num >= 0 Then
MsgBox(0, "Result", "VALID")
Else
MsgBox(0, "Result", "NOT VALID")
EndIf

works for me

[right][font="Courier New"]...Run these streets all day, I can sleep when I die.[/font] [/right]

Link to comment
Share on other sites

$Num = InputBox("Number Checker", "Please enter a VALID number greater than or equal to zero.","0")
If $Num >= 0 Then
MsgBox(0, "Result", "VALID")
Else
MsgBox(0, "Result", "NOT VALID")
EndIf

works for me

Thanks! ;)

Actually, I tried that before. It is not 100% working for me, because if I enter other characters (not numbers), it will still return "VALID"

This is what I have in mind, but it doesn't work! :P

$Num = InputBox("Number Checker", "Please enter a VALID number greater than or equal to zero.","0")
If IsNumber($Num) And $Num >= 0 Then
MsgBox(0, "Result", "VALID")
Else
MsgBox(0, "Result", "NOT VALID")
EndIf

Could you please tell me how to implement the IsNumber? Because that code above always return "NOT VALID".

Thanks again!

Edited by totoymola
Link to comment
Share on other sites

Thanks! ;)

Actually, I tried that before. It is not 100% working for me, because if I enter other characters (not numbers), it will still return "VALID"

what you need is StringIsInt().

$Num = InputBox("Number Checker", "Please enter a VALID number greater than or equal to zero.","0")
If StringIsInt($Num) And $Num >= 0 Then
   MsgBox(0, "Result", "VALID")
Else
   MsgBox(0, "Result", "NOT VALID")
EndIf

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

what you need is StringIsInt().

$Num = InputBox("Number Checker", "Please enter a VALID number greater than or equal to zero.","0")
If StringIsInt($Num) And $Num >= 0 Then
   MsgBox(0, "Result", "VALID")
Else
   MsgBox(0, "Result", "NOT VALID")
EndIf

Cheers

Kurt

Wow!! That solved my problem! Thanks a lot Kurt and B3TA_SCR1PT3R! ;)

I'm a total n00b that's why I don't know how to use that function (yet). :P

By the way, if it's not too much to ask, can you give me examples of using the IsNumber function? The help file doesn't give much info about it.

Thanks!

Link to comment
Share on other sites

By the way, if it's not too much to ask, can you give me examples of using the IsNumber function? The help file doesn't give much info about it.

From help file: IsNumber() checks if a variable's base type is numeric. As InputBox() allways returns a string, your if clause failed (IsNumber($Num) is allways 0 then!).

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Thanks fro all your help. ;)

This is my final working code (I hope).

$Num = InputBox("Number Checker", "Please enter a VALID number greater than or equal to zero.", "0")
If (StringIsInt($Num) Or StringIsFloat($Num)) And $Num >= 0 Then
    MsgBox(0, "Result", "VALID")
Else
    MsgBox(0, "Result", "NOT VALID")
EndIf

I added the StringIsFloat() so it will accept numbers with decimals as well. Everything is working fine so far, but I'm curious to know the other methods of doing this too.

:P

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...