Jump to content

How to filter user input for digits?


Fire
 Share

Recommended Posts

Hi to all.

I need small help.

My trouble is how can i filter user input (ON GUI) mode.

For example i want to allow only digits dont allow letter.

How to do it?

:

$a = GUICtrlCreateInput("", 192, 144, 153, 21)  
GUICtrlSetLimit(-1, 12)
[size="5"] [/size]
Link to comment
Share on other sites

You can add the $ES_NUMBER style to the input control:

#include <GuiConstantsEx.au3>
#include <EditConstants.au3>

$hGUI = GUICreate("Test", 200, 200)
$idInput = GUICtrlCreateInput("", 20, 20, 160, 20, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
But that doesn't allow negatives or decimal points, and you can't set limits on it.

Perhaps you want an UpDown control instead? See the example in the help file under GUICtrlCreateUpdown().

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Or you use this Function StringIsDigit to check the input after it's been inputted.

Thank you very much snowmaker.I learn this way too from you.

And i understand that for inside out use StringIsAlpha(). (allow only alpahebitcal input)

Than you very much PsaltyDS.

Really Respects ++

[size="5"] [/size]
Link to comment
Share on other sites

  • Moderators

Sh3llC043r,

If you want an input which will accept negatives and decimal points, here is one that will: :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

GUICreate("Input Filter", 300, 30, -1, -1)
Global $inTest = GUICtrlCreateInput("", 5, 5, 290)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd

GUIRegisterMsg($WM_COMMAND, "")

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16)    ;HiWord

    If $iIDFrom = $inTest And $iCode = $EN_CHANGE Then

    $Read_Input = GUICtrlRead($inTest)
    If StringRegExp($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]') Then $Read_Input = StringRegExpReplace($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]', '\1')
    $Point1 = StringInStr($Read_Input, ".", 0)
    $Point2 = StringInStr($Read_Input, ".", 0, 2)
    If $Point2 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point2 - 1)
    If $Point1 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point1 + 2)
    GUICtrlSetData($inTest, $Read_Input)

    EndIf

EndFunc;==>_WM_COMMAND

It is pretty easy to adapt to any munber of decimal places - an exercise for the student as my old maths master used to say! ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you very much snowmaker.I learn this way too from you.

I hope whatever method you use suits your needs. I'm not much of a coder at all, I learn by trying to help others, and usually my suggestions are not the best way, and sometimes they're just plain wrong..:)

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Link to comment
Share on other sites

Sh3llC043r,

If you want an input which will accept negatives and decimal points, here is one that will:

Your code is Great.Than you very much too.I think all this post is sholud be very helpness for newbies as like me .

Thanks to all for help :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

GUICreate("Input Filter", 300, 30, -1, -1)
Global $inTest = GUICtrlCreateInput("", 5, 5, 290)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd

GUIRegisterMsg($WM_COMMAND, "")

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16)    ;HiWord

    If $iIDFrom = $inTest And $iCode = $EN_CHANGE Then

    $Read_Input = GUICtrlRead($inTest)
    If StringRegExp($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]') Then $Read_Input = StringRegExpReplace($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]', '\1')
    $Point1 = StringInStr($Read_Input, ".", 0)
    $Point2 = StringInStr($Read_Input, ".", 0, 2)
    If $Point2 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point2 - 1)
    If $Point1 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point1 + 2)
    GUICtrlSetData($inTest, $Read_Input)

    EndIf

EndFunc;==>_WM_COMMAND
[size="5"] [/size]
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...