Jump to content

Validating a field to make sure that only numbers are entered


Recommended Posts

Hi!

i've been tring to add some "smarts" into my script but it doesn't work 100%. What i want to do is if the user has entered anything but a number into the field the script should stop and tell them to try again:

;Buton in my gui which starts the validation process and other stuff

While 1
    $nMsg = GUIGetMsg()
    If Not WinActive($MatterCreationForm_1) Then WinActivate($MatterCreationForm_1)

; to select a specific default format
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Sleep(300)
            Run("F:\Affinity\Affinity Script Resources\Old Affinity Scripts\Matter Creation reset.exe")
            Exit
        Case $ButtonPopulate
            Validation()
        If $validation = 0 Then
            BlockInput(1)
            WinSetState("New Matter Creation Form", "", @SW_MINIMIZE)
            WinActivate("Matter Creation")
            Sleep(200)
            Opt("SendKeyDelay", 10)
            MatterDetails()
            Fdate()
            Sdescription()
            Ldescription()
            Financial()
            Billing()
            Contacts()
            Sleep(300)
            Run("F:\Affinity\Affinity Script Resources\New Affinity Scripts\Matter Creation reset.exe")
            Exit
        ElseIf $Validation = 1 Then
            WinSetOnTop ("New Matter Creation Form", "", 0)
            MsgBox(16, "STOP", "Not all the required fields have been enetered, please fill out the required fields (*) and try again.")
            WinSetOnTop ("New Matter Creation Form", "", 1)
        ElseIf $validation = 2 Then
            WinSetOnTop ("New Matter Creation Form", "", 0)
            MsgBox(16, "STOP", "You can only enter in numbers in the Financial section. Please amend and try again")
            WinSetOnTop ("New Matter Creation Form", "", 1)

        EndIf

        Case $MenuAbout
            WinSetOnTop ("New Matter Creation Form", "", 0)
            MsgBox(0, "About", "Written By: Hilmy Abdic V1.0")
            WinSetOnTop ("New Matter Creation Form", "", 1)
        Case $MenuExit
            Sleep(300)
            Run("F:\Affinity\Affinity Script Resources\Old Affinity Scripts\Matter Creation reset.exe")
            Exit

    EndSwitch

WEnd

;pointless code in this section

func Validation()
        $validation = 0
        If GUICtrlRead($InputSDescrition) = "" Then
        $validation = 1
        ElseIf GUICtrlRead($InputLDescrition) = "" Then
        $validation = 1
        ElseIf Not Number(GUICtrlRead($InputFees)) Then
        $validation = 2
        ElseIf Not Number(GUICtrlRead($InputDisb)) Then
        $validation = 2
        EndIf

EndFunc

So as you can see in the validation function I have:

ElseIf Not Number(GUICtrlRead($InputDisb)) Then
        $validation = 2

if a user enters a letter in such as "abc" it will validate and say STOP blah blah blah try again. but if they enter "123abc" it runs the rest of the code.

Does anyone know how i can fix this?

Thanks!

Edited by PieMan
Link to comment
Share on other sites

The problem is, you're reading Strings, not Numbers when you use GUICtrlRead.

Use something like this

StringIsInt(GUICtrlRead($InputDescrition))

I don't have the entire script, so I can't test it out for you. But hopefully you get the basic idea. Post your entire script and i can fix it for you.

btw, itd probably be easier/more professional to do that using 'return's in the Validation function instead of saving it in the $validation variable.

Link to comment
Share on other sites

Hi!

i've been tring to add some "smarts" into my script but it doesn't work 100%. What i want to do is if the user has entered anything but a number into the field the script should stop and tell them to try again:

;Buton in my gui which starts the validation process and other stuff
  
  While 1
      $nMsg = GUIGetMsg()
      If Not WinActive($MatterCreationForm_1) Then WinActivate($MatterCreationForm_1)
  
; to select a specific default format
      Switch $nMsg
          Case $GUI_EVENT_CLOSE
              Sleep(300)
              Run("F:\Affinity\Affinity Script Resources\Old Affinity Scripts\Matter Creation reset.exe")
              Exit
          Case $ButtonPopulate
              Validation()
          If $validation = 0 Then
              BlockInput(1)
              WinSetState("New Matter Creation Form", "", @SW_MINIMIZE)
              WinActivate("Matter Creation")
              Sleep(200)
              Opt("SendKeyDelay", 10)
              MatterDetails()
              Fdate()
              Sdescription()
              Ldescription()
              Financial()
              Billing()
              Contacts()
              Sleep(300)
              Run("F:\Affinity\Affinity Script Resources\New Affinity Scripts\Matter Creation reset.exe")
              Exit
          ElseIf $Validation = 1 Then
              WinSetOnTop ("New Matter Creation Form", "", 0)
              MsgBox(16, "STOP", "Not all the required fields have been enetered, please fill out the required fields (*) and try again.")
              WinSetOnTop ("New Matter Creation Form", "", 1)
          ElseIf $validation = 2 Then
              WinSetOnTop ("New Matter Creation Form", "", 0)
              MsgBox(16, "STOP", "You can only enter in numbers in the Financial section. Please amend and try again")
              WinSetOnTop ("New Matter Creation Form", "", 1)
  
          EndIf
  
          Case $MenuAbout
              WinSetOnTop ("New Matter Creation Form", "", 0)
              MsgBox(0, "About", "Written By: Hilmy Abdic   V1.0")
              WinSetOnTop ("New Matter Creation Form", "", 1)
          Case $MenuExit
              Sleep(300)
              Run("F:\Affinity\Affinity Script Resources\Old Affinity Scripts\Matter Creation reset.exe")
              Exit
  
      EndSwitch
  
  WEnd
  
;pointless code in this section
  
  func Validation()
          $validation = 0
          If GUICtrlRead($InputSDescrition) = "" Then
          $validation = 1
          ElseIf GUICtrlRead($InputLDescrition) = "" Then
          $validation = 1
          ElseIf Not Number(GUICtrlRead($InputFees)) Then
          $validation = 2
          ElseIf Not Number(GUICtrlRead($InputDisb)) Then
          $validation = 2
          EndIf
  
  EndFunc

So as you can see in the validation function I have:

ElseIf Not Number(GUICtrlRead($InputDisb)) Then
          $validation = 2

if a user enters a letter in such as "abc" it will validate and say STOP blah blah blah try again. but if they enter "123abc" it runs the rest of the code.

Does anyone know how i can fix this?

Thanks!

It might be easier to use the style $ES_NUMBER for the input.

Or you could register $EN_CHANGE and prevent unwanted characters being accepted.

To check that the string is all numbers you could say

$Val = StringStripWS(GuiCtrlRead($InputDisb),3)
 If String(Number($val)) = $Val then;it must be just a number

EDIT: CrewXp's post reminded me of the StringIsNumber and StringIsDigit functions whichs makes my code a bit daft.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I'm guessing your trying to stop the user from typing anything that is not a number.. well if so then you should of looked in the help file because assuming your using a GUI input box then.. 0x2000 can be used as style and it will stop users from typing in anything that is not a number.

Example :

#Include <GUIConstantsEx.Au3>
#Include <WindowsConstants.Au3>
Opt ('GUIOnEventMode','1')
$Style = '0x2000'

$GUI = GUICreate ('Example','200','19','-1','-1','-1','128')
GUISetOnEvent ($GUI_EVENT_CLOSE, '_Exit')
$Input = GUICtrlCreateInput ('','0','0','200','20', $Style)
GUISetState (@SW_SHOW, $GUI)

While '1'
Sleep ('150')
WEnd

Func _Exit ()
Exit
EndFunc

Hope this helps!

- John

Edit : I made the post before I seen the above one...

Edited by John2006

Latest Projects :- New & Improved TCP Chat

Link to comment
Share on other sites

Thank you so much, it worked like a charm!

Im still new to programming and AutoIt so right now im just playing around with it and learning its functions. Ill look into using returns next time for my functions.

Thanks again!

EDIT: Thanks for the response John. Ill also look into using style too. Thanks.

Edited by PieMan
Link to comment
Share on other sites

it doesnt seem to work with decimal points... any ideas?

It?

If you mean StringIsInt then maybe StringIsDigit.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

It?

If you mean StringIsInt then maybe StringIsDigit.

sorry. StringIsInt doesnt work with decimal points. Same with StringIsDigit.

Here is all my code... its a bit messy and there are probably better ways to code it too.

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=F:\Affinity\Affinity Script Resources\icon.ico
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.0.0
    Author:      Hilmy Abdic
                Version: 1.0

    Script Function: Automate the process for creating matters
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

WinWaitActive("Matter Creation")


$destination = "F:\Affinity\Affinity Script Resources\affinityB.JPG"

SplashImageOn("Splash Screen", $destination,288,236)
WinSetOnTop("Splash Screen", "", 1)
Sleep(3000)
SplashOff()

Global $Date, $DTM_SETFORMAT_, $style, $validation, $Alpha, $Sinput, $Linput, $Value, $InputDisb1

#include <ButtonConstants.au3>
#include <DateTimeConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ScreenCapture.au3>

; Capture full screen
;but don't capture mouse
;_ScreenCapture_Capture ("screen1.jpg", 0, 0, @DesktopWidth, @DesktopHeight, False)

;$fake = GUICreate("fake", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP)
;$Img = GUICtrlCreatePic("screen1.jpg", 0, 0, @DesktopWidth, @DesktopHeight)

;show but without flickering
;WinSetTrans($fake, "", 0)
;GUISetState()
;WinSetTrans($fake, "", 255)

;now the gui which is in sole control

;$stylenum = '0x2000'

#Region ### START Koda GUI section ### Form=
$MatterCreationForm_1 = GUICreate("New Matter Creation Form", 485, 555, 192, 114)
$MenuFile = GUICtrlCreateMenu("&File")
$MenuClear = GUICtrlCreateMenuItem("Clear Fields", $MenuFile)
$MenuExit = GUICtrlCreateMenuItem("Exit", $MenuFile)
$MenuHelp = GUICtrlCreateMenu("&Help")
$MenuAbout = GUICtrlCreateMenuItem("About", $MenuHelp)
$Label1 = GUICtrlCreateLabel("Instructed:", 8, 32, 54, 17)
$Label2 = GUICtrlCreateLabel("Matter Creatrion", 168, 8, 79, 17)
$Date = GUICtrlCreateDate("2009/06/15 13:44:58", 112, 32, 186, 21)
$InputAlpha = GUICtrlCreateInput("", 112, 64, 121, 21)
$InputSDescrition = GUICtrlCreateEdit("", 112, 96, 289, 57)
GUICtrlSetData(-1, "")
$InputLDescrition = GUICtrlCreateEdit("", 112, 168, 289, 81)
GUICtrlSetData(-1, "")
$InputRef = GUICtrlCreateInput("", 112, 272, 121, 21)
$Label4 = GUICtrlCreateLabel("Short Description:", 8, 88, 88, 17)
$Label5 = GUICtrlCreateLabel("Long Description:", 8, 168, 87, 17)
$Group1 = GUICtrlCreateGroup("Financial", 17, 305, 255, 151)
$Label7 = GUICtrlCreateLabel("Expected Value:", 24, 320, 85, 17)
$Label8 = GUICtrlCreateLabel("Credit Limits:", 72, 352, 63, 17)
$Label9 = GUICtrlCreateLabel("Fees:", 24, 376, 30, 17)
$Label10 = GUICtrlCreateLabel("Disbursements:", 24, 400, 76, 17)
$Label11 = GUICtrlCreateLabel("Overall Limit:", 24, 424, 64, 17)
$InputEValue = GUICtrlCreateInput("", 112, 320, 73, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$InputFees = GUICtrlCreateInput("", 109, 373, 73, 21)
$InputDisb = GUICtrlCreateInput("", 109, 397, 73, 21)
$Label13 = GUICtrlCreateLabel("(Optional)", 208, 392, 49, 17)
$InputOverLimit = GUICtrlCreateInput("", 109, 421, 73, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$ChkAuth = GUICtrlCreateCheckbox("Authority", 192, 376, 65, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Billing = GUICtrlCreateGroup("Billing", 280, 304, 169, 153)
$RadioTaxBill = GUICtrlCreateRadio("Taxable Bill", 288, 352, 113, 17)
$Label3 = GUICtrlCreateLabel("Tax Status for New  Bills:", 288, 328, 122, 17)
$RadioExportBill = GUICtrlCreateRadio("Export Bill", 288, 385, 113, 17)
$RadioOther = GUICtrlCreateRadio("Other Tax-free Bill", 288, 416, 113, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$ButtonPopulate = GUICtrlCreateButton("Populate Fields", 184, 480, 123, 33, $WS_GROUP)
$Label6 = GUICtrlCreateLabel("Alpha (Optional):", 8, 64, 96, 17)
$Label12 = GUICtrlCreateLabel("Your Ref. (Optional):", 8, 272, 100, 17)
GUISetState(@SW_SHOW)
Dim $MatterCreationForm_1_AccelTable[1][2] = [["{NUM 5}", $MenuExit]]
GUISetAccelerators($MatterCreationForm_1_AccelTable)
#EndRegion ### END Koda GUI section ###


; to select a specific default format for Date
$DTM_SETFORMAT_ = 0x1032
$style = "dd/MM/yyyy"
GUICtrlSendMsg($Date, $DTM_SETFORMAT_, 0, $style)

;Sets radio button to be checked at load
GuiCtrlSetState($RadioTaxBill, $GUI_CHECKED)

WinSetOnTop ("New Matter Creation Form", "", 1)

While 1
    $nMsg = GUIGetMsg()
    If Not WinActive($MatterCreationForm_1) Then WinActivate($MatterCreationForm_1)

; to select a specific default format
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Sleep(300)
            Run("F:\Affinity\Affinity Script Resources\Old Affinity Scripts\Matter Creation reset.exe")
            Exit
        Case $ButtonPopulate
            Validation()
        If $validation = 0 Then
            BlockInput(1)
            WinSetState("New Matter Creation Form", "", @SW_MINIMIZE)
            WinActivate("Matter Creation")
            Sleep(200)
            Opt("SendKeyDelay", 10)
            MatterDetails()
            Fdate()
            Sdescription()
            Ldescription()
            Financial()
            Billing()
            Contacts()
            Sleep(300)
            Run("F:\Affinity\Affinity Script Resources\New Affinity Scripts\Matter Creation reset.exe")
            Exit
        ElseIf $Validation = 1 Then
            WinSetOnTop ("New Matter Creation Form", "", 0)
            MsgBox(16, "STOP", "Not all the required fields have been enetered, please fill out the required fields (*) and try again.")
            WinSetOnTop ("New Matter Creation Form", "", 1)
        ElseIf $validation = 2 Then
            WinSetOnTop ("New Matter Creation Form", "", 0)
            MsgBox(16, "STOP", "You can only enter in numbers in the Financial section. Please amend and try again")
            WinSetOnTop ("New Matter Creation Form", "", 1)

        EndIf

        Case $MenuAbout
            WinSetOnTop ("New Matter Creation Form", "", 0)
            MsgBox(0, "About", "Written By: Hilmy Abdic V1.0")
            WinSetOnTop ("New Matter Creation Form", "", 1)
        Case $MenuExit
            Sleep(300)
            Run("F:\Affinity\Affinity Script Resources\Old Affinity Scripts\Matter Creation reset.exe")
            Exit

    EndSwitch

WEnd

    func Validation()
        $validation = 0
        If GUICtrlRead($InputSDescrition) = "" Then
        $validation = 1
        ElseIf GUICtrlRead($InputLDescrition) = "" Then
        $validation = 1
        ElseIf Not StringIsDigit(GUICtrlRead($InputFees)) Then
        $validation = 2
        ElseIf Not StringIsDigit(GUICtrlRead($InputDisb)) Then
        $validation = 2
        EndIf

    EndFunc

    Func MatterDetails()
        ControlClick("Matter Creation", "", "TBitBtn1")
        WinWaitActive("Code Selection", "")
        BlockInput(0)
        MsgBox(64, "Partner", "Please select the managing Partner for this Matter")
        WinWaitClose("Code Selection", "")
        BlockInput(1)
        WinActivate("Matter Creation")

        ControlClick("Matter Creation", "", "TBitBtn2")
        WinWaitActive("Code Selection", "")
        BlockInput(0)
        MsgBox(64, "Controller", "Please select the Controller for this Matter")
        WinWaitClose("Code Selection", "")
        BlockInput(1)
        WinActivate("Matter Creation")

        ControlClick("Matter Creation", "", "TBitBtn3")
        WinWaitActive("Code Selection", "")
        BlockInput(0)
        MsgBox(64, "Author", "Please select the Author for this Matter")
        WinWaitClose("Code Selection", "")
        BlockInput(1)
        WinActivate("Matter Creation")

        ControlClick("Matter Creation", "", "TBitBtn4")
        WinWaitActive("Code Selection", "")
        BlockInput(0)
        MsgBox(64, "Type", "Please select the Type for this Matter")
        WinWaitClose("Code Selection", "")
        BlockInput(1)
        WinActivate("Matter Creation")

    EndFunc

    Func Fdate()
        ControlClick("Matter Creation", "", "TPageControl1", "left", 1, 30, 10)
        ControlClick("Matter Creation", "", "TDateTimePicker1", "left", 1, 9, 10)
        Send(GUICtrlRead($Date))
        Send("{TAB}")
        Send("{TAB}")
    EndFunc  ;==>Fdate

    Func Sdescription()
        $Alpha = GUICtrlRead($InputAlpha)
        Send($Alpha)
        Send("{TAB}")
        $Sinput = StringUpper(GUICtrlRead($InputSDescrition))
        Send($Sinput)
        Send("{TAB}")
        send("^a")
        send("{BS}")
    EndFunc  ;==>Sdescription

    Func Ldescription()
        $Linput = StringUpper(GUICtrlRead($InputLDescrition))
        Send($Linput)
        Send("{TAB}")
        $Ref = GUICtrlRead($InputRef)
        Send($ref)
        Send("{TAB 7}")
    EndFunc  ;==>Ldescription

    Func Financial()
        $Value = GUICtrlRead($InputFees)
        Send($Value)
        Send("{TAB 2}")
        Send($Value)
        Send("{TAB}")
        $InputDisb1 = GUICtrlRead($InputDisb)
        Send($InputDisb1)
        $total = $Value + $InputDisb1
        Send("{TAB}")
        Send($total)
        If GUICtrlRead($ChkAuth) = 1 Then
            ControlClick("Matter Creation", "", "TDBCheckBox2")
            Send("+{TAB 3}")
        Else
        EndIf
    EndFunc

    Func Billing()
        Send("+{TAB 6}")
        Send("{RIGHT 2}")
        ControlClick("Matter Creation", "", "TGroupButton4")
        Send("+{TAB 4}")
        Send("{RIGHT 2}")
        Send("{TAB 4}")
    If GUICtrlRead($RadioTaxBill) = 1 Then
    ElseIf GUICtrlRead($RadioExportBill) = 1 Then
        Send("{DOWN}")
    ElseIf GUICtrlRead($RadioOther) = 1 Then
        Send("{DOWN 2}")
    EndIf
    EndFunc

    Func Contacts()
        Send("+{TAB 4}")
        Send("{LEFT 3}")
        BlockInput(0)
        MsgBox(64, "Nearly Done...", "Please fill out all the fields in the Contacts Tab (Bill To, Contact, Referred By, Bill To Contact), Review the information and Submit. Your Done! ")

    EndFunc
Link to comment
Share on other sites

replace your Validation function with this:

func Validation()
        $validation = 0
        If GUICtrlRead($InputSDescrition) = "" Then
        $validation = 1
        ElseIf GUICtrlRead($InputLDescrition) = "" Then
        $validation = 1
        ElseIf Not StringIsDigit(GUICtrlRead($InputFees)) Then
            If Not StringIsFloat(GUICtrlRead($InputFees)) Then 
            $validation = 2
            EndIf
        ElseIf Not StringIsDigit(GUICtrlRead($InputDisb)) Then
            If Not StringIsFloat(GUICtrlRead($InputDisb)) Then 
            $validation = 2
            EndIf
        EndIf

    EndFunc
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...