Jump to content
Sign in to follow this  
Fire

How to filter user input for digits?

Recommended Posts

Fire

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]

Share this post


Link to post
Share on other sites
Mat

have you tried the $ES_NUMBER style?

$ES_NUMBER  |  0x2000  |  Accepts into the edit control only digits to be typed.

Edit styles can be used on inputs, as inputs are simply edits without the $ES_MULTILINE style.

Mat

Share this post


Link to post
Share on other sites
Fire

Thanks Mat.I tried StringRegExpReplace() but pattern is very difficult for me using.

Thanks for alternative method i would try it Thanks again.


[size="5"] [/size]

Share this post


Link to post
Share on other sites
PsaltyDS

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

Share this post


Link to post
Share on other sites
Fire

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]

Share this post


Link to post
Share on other sites
Melba23

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
somdcomputerguy

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.

Share this post


Link to post
Share on other sites
Fire

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]

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.