Jump to content

inputbox to have only 3 digit number


Recommended Posts

I have one that I can't figure out. I have looked around in other threads and I did find some ideas but nothing has worked. What I want is an input box that will only allow a 3 digit number. No blank line, no letters or any other characters. It works when I use 999 and it will loop if I put in 4444 but it will spit out a correct line as a 9k9. That is what I am having problems with. I don't want that but I don't seem to know what I am doing wrong. I think that the StringRegExp and the IsNumber are doing the same thing. Thanks for any help.

DirCreate(@ScriptDir&"\test")



$file = FileOpen(@ScriptDir&"\test\pc_num.txt", 0);open pc_num.txt file

If $file = -1 Then

    MsgBox(16, "Error", "Unable to open pc_num.txt file.")

    Do

        MsgBox(16,"Error","The PC number can not be blank or not a number and only 3 digits.  Please input the PC number.")

        $pcnum = InputBox("New PC number","What is the PC number? ","","",190,115)

    Until StringRegExp($pcnum,"^[0-9]") And StringLen($pcnum)=3 or IsNumber($pcnum)  <--problem is this line

    $file1 = FileOpen(@ScriptDir&"\test\pc_num.txt", 1);new school_num.txt file

    FileWriteLine($file1,$pcnum)

    FileClose($file1);for new pc_num.txt

    $file = FileOpen(@ScriptDir&"\test\pc_num.txt", 0);open school_num.txt file

EndIf

$pcnum = FileReadLine($file)

FileClose($file);for pc_num.txt




MsgBox(0,"","PC Number _:"&$pcnum)



DirRemove(@ScriptDir&"\test",1)

RUN . . . Slide . . . TAG . . . Your out . . . PAINTBALL !!!

Link to comment
Share on other sites

This regular expression should be the same as checking IsNumber() and StringLen().

Do
$pcnum = InputBox("New PC number","What is the PC number? ","","",190,115)
Until StringRegExp($pcnum, "\A(\d){3}$", 0)
Link to comment
Share on other sites

This is my slightly Rube-Goldbergian approach:

$nNum = InputBox("Enter a 3 Digit Number", "Enter any 3 Digit Number")
$sz3Dig = StringRegExpReplace($nNum, "[^0-9]", "")
If ($sz3Dig = $nNum AND StringLen($sz3Dig) = 3) Then
    MsgBox(0, "Good", "Good. That was a 3 Digit Number")
Else
    MsgBox(0, "Error", "That's not a 3 digit number you dunce")
EndIf

It's not pretty, but it works.

Link to comment
Share on other sites

Your stringregexp to only accept 3 numbers would look like this:

StringRegExp($a,"^[0-9][0-9][0-9]$")

You don't need the IsNumber or StringLen with that, it only accepts 3 characters between 0 and 9.

Thank you. Thank you Thank you. That works. The only question is what does the $ mean and what does it do? I did not see it in the help files.

Thanks again.

RUN . . . Slide . . . TAG . . . Your out . . . PAINTBALL !!!

Link to comment
Share on other sites

Thank you. Thank you Thank you. That works. The only question is what does the $ mean and what does it do? I did not see it in the help files.

Thanks again.

The "$" signifies the end of the string to be checked. Just as the "^" means the begining

So a string that begins, has 3 numerical characters and ends, also known as a 3 digit number will match that RegEx.

However, you may want to make the first one a [1-9]

StringRegExp($a,"^[1-9][0-9][0-9]$")

otherwise a number like 099 can get through

Edited by Paulie
Link to comment
Share on other sites

Thanks for the answers. I haven't used StringRegExp much before so I am learning the importance of it now. It is a very powerful tool in many different ways. Thanks again for all the help and it was so fast too.

RUN . . . Slide . . . TAG . . . Your out . . . PAINTBALL !!!

Link to comment
Share on other sites

Don't remember who wrote it but here is another way...

#include <GUIConstants.au3>

GUICreate("My GUI limit input 3 chars")  ; will create a dialog box that when displayed is centered

GUICtrlCreateInput("", 10,20)
GUICtrlSetLimit(-1,3) ; to limit the entry to 3 chars
    
GUISetState ()

; Run the GUI until the dialog is closed
While 1
 $msg = GUIGetMsg()
 
 If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend
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...