Sign in to follow this  
Followers 0
saldous

How to force a minimum number of characters in InputBox?

10 posts in this topic

Hi, I hope someone can help me here:

When using InputBox you can set a maximum number of characters that can be entered, but I can't find a way to set a minimum number, is this possible?

Also, for example I'm asking a user to enter their email address, but I have no way to check what is entered follows an email address format, e.g does the text entered contain an "@".

Thank you for any help.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

If you are using a GUI then GUICtrlSetLimit() will help you here.

Edit:

Also, if I'm not mistaken, I've posted a _IsValidEmail() function before...

Edit2:

Yes, here it is:

http://www.autoitscript.com/forum/index.ph...st&p=301465

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Excellent, thanks SmOke N. I can't get it to work though... Here is my code:

CODE
; User enters email address

$email = InputBox("Email checker", "Please enter your email address", "", " M", _

400, -1)

;is the email address valid?

Func _IsVailidEmail($email)

If StringRegExp($email, "^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|" & _

"((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$") Then Return 1

Return SetError(1, 0, 0)

If @error = 1 Then

msgbox(4096,"Invalid Email Address", "The email address you entered is not valid, please try again")

else

msgbox(4096,"Correct Email Address", "The email address you entered is OK")

Endif

EndFunc

What have I done wrong? I don't seem to be getting an error when it fails the check, neither of my message boxes appear.

Edited by saldous

Share this post


Link to post
Share on other sites

anyone?

Share this post


Link to post
Share on other sites

You never actually called the function:

; User enters email address
$email = InputBox("Email checker", "Please enter your email address", "", " M", _
400, -1)
If _IsValidEmail($email) Then
    msgbox(4096,"Correct Email Address", "The email address you entered is OK")
Else
    msgbox(4096,"Invalid Email Address", "The email address you entered is not valid, please try again")
EndIf

;is the email address valid?
Func _IsValidEmail($email)
    If StringRegExp($email, "^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|" & _
    "((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$") Then Return 1
    Return SetError(1, 0, 0)
EndFunc


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Ah ha, I get ya, thanks for fixing this, it works perfectly!

Share this post


Link to post
Share on other sites

How about making an input box with a minimum number of characters to be entered though? i can set a max, but not a min. I need to to have a min of 6.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

How about making an input box with a minimum number of characters to be entered though? i can set a max, but not a min. I need to to have a min of 6.

Make a GUI.

Edit:

I already did an _InputBox GUI and I found it:

#include <guiconstants.au3>
MsgBox(0, 'Info', _InPutBoxEx('My Input Box'))
MsgBox(0, 'Info', _InPutBoxEx('My Input Box', 'Some Text', 300))



Func _InPutBoxEx($sTitle, $sText = '', $iWidth = 200, $iHeight = 75, $iOnTop = 1, $nInStyle = -1, $nInStyleEx = -1)
    Local $aCID[5], $iY = 0, $sRead
    If $iWidth < 175 Then $iWidth = 175
    If $iHeight < 75 Then $iHeight = 75
    If $sText <> '' Then $iY = 20
    Local $hGUI = GUICreate($sTitle, $iWidth, $iHeight + $iY)
    If $sText <> '' Then $aCID[1] = GUICtrlCreateLabel($sText, 10, 10, $iWidth - 20, 20, $ES_CENTER)
    $aCID[2] = GUICtrlCreateInput('', 10, 10 + $iY, $iWidth - 20, 20, $nInStyle, $nInStyleEx)
    $aCID[3] = GUICtrlCreateButton('OK', (($iWidth - 150) / 2), 40 + $iY, 75, 25, $BS_DEFPUSHBUTTON)
    $aCID[4] = GUICtrlCreateButton('Cancel', (($iWidth - 150) / 2) + 80, 40 + $iY, 75, 25)
    WinSetOnTop($hGUI, '', $iOnTop)
    GUISetState()
    While 1
        Switch GUIGetMsg()
            Case - 3, $aCID[4]
                GUIDelete($hGUI)
                Return ''
            Case $aCID[3]
                $sRead = GUICtrlRead($aCID[2])
                GUIDelete($hGUI)
                Return $sRead
        EndSwitch
    WEnd
EndFunc
Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Thank you, but I can't get this to work, I can enter less than 6 characters and it still accepts it.

Share this post


Link to post
Share on other sites

Thank you, but I can't get this to work, I can enter less than 6 characters and it still accepts it.

I guess the thought of you trying to mod it yourself isn't actually an option?
#include <guiconstants.au3>
MsgBox(0, 'Info', _InPutBoxEx('My Input Box'))
MsgBox(0, 'Info', _InPutBoxEx('My Input Box', 'Some Text', 300))



Func _InPutBoxEx($sTitle, $sText = '', $iWidth = 200, $iHeight = 75, $iOnTop = 1, $nInStyle = -1, $nInStyleEx = -1)
    Local $aCID[5], $iY = 0, $sRead
    If $iWidth < 175 Then $iWidth = 175
    If $iHeight < 75 Then $iHeight = 75
    If $sText <> '' Then $iY = 20
    Local $hGUI = GUICreate($sTitle, $iWidth, $iHeight + $iY)
    If $sText <> '' Then $aCID[1] = GUICtrlCreateLabel($sText, 10, 10, $iWidth - 20, 20, $ES_CENTER)
    $aCID[2] = GUICtrlCreateInput('', 10, 10 + $iY, $iWidth - 20, 20, $nInStyle, $nInStyleEx)
    $aCID[3] = GUICtrlCreateButton('OK', (($iWidth - 150) / 2), 40 + $iY, 75, 25, $BS_DEFPUSHBUTTON)
    $aCID[4] = GUICtrlCreateButton('Cancel', (($iWidth - 150) / 2) + 80, 40 + $iY, 75, 25)
    WinSetOnTop($hGUI, '', $iOnTop)
    GUISetState()
    While 1
        Switch GUIGetMsg()
            Case - 3, $aCID[4]
                GUIDelete($hGUI)
                Return ''
            Case $aCID[3]
                $sRead = GUICtrlRead($aCID[2])
                If StringLen($sRead) <= 10 And StringLen($sRead) >= 6 Then
                    GUIDelete($hGUI)
                    Return $sRead
                ElseIf StringLen($sRead) > 10 Then
                    MsgBox(262144 + 16, 'Error', 'You have entered too many characters.')
                ElseIf StringLen($sRead) < 6 Then
                    MsgBox(262144 + 16, 'Error', 'You did not enter enough characters.')
                EndIf
        EndSwitch
    WEnd
EndFunc

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
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
Sign in to follow this  
Followers 0