Jump to content

StringRegExp (no - mix)


Recommended Posts

Hi,

is there someway to set a controls like combo or input accept only English and number characters
As I'm not planning to mess with CHCP With Lucida Console or other font support CMD's language codes

Maybe with $CBN_EDITCHANGE

So, what should I use to check if only A-z + numbers are to be accepted ? (with no other mix characters)

Thanks

Edited by Deye
Link to comment
Share on other sites

StringRegExp($input, '(?i)^[a-z\d]*$')

should do it, returning a boolean True if accepted.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

You could add the three new characters to jchd's exclusion list, and do a preliminary check for double+ spaces

$input="73 8249 572a sdf78 -_-"

If stringstripWS($input , 4) = $input Then
   $sOut = StringRegExp($input, '(?i)^[a-z\d\-\_\s]*$')
Else
   $sOut = 0
EndIf

msgbox(0, '' , $sOut)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

This example only allows into the input control the following characters:-
 "a to z" upper and lower case;
 "0 to 9" numbers;
 One underscore "_", one minus sign, "-", or one space, " ", at a time, and, only after any letter\s or digit\s.
This will not allow an underscore, a minus sign, or a space at the beginning of the input string.  

Examples:-
"_Abc12" not allowed because underscore is not after a letter or digit;
"A-_bc 12" not allowed because underscore is not after a letter or digit;
"Abc__12" not allowed because there are two consecutive underscores;
"Abc-12 34_De_" allowed.

Note - If you give examples like this (what you want, and what you don't want), it would be easier to understand and help you.

#include <GUIConstants.au3>
#include <WinApi.au3>
; Modified from https://www.autoitscript.com/forum/topic/187551-custom-formating-text-in-inputbox-possible/?do=findComment&comment=1349344

GUIRegisterMsg($WM_COMMAND, _WM_COMMAND)

Global $sStr = _FormatedInput() ; Get formated data from input control.
MsgBox(0, "Returned Results", $sStr)


Func _FormatedInput()
    Local $Res
    Global $hFormatedInput = GUICreate("Example", 300, 150)
    Global $idInput = GUICtrlCreateInput("", 10, 10, 180, 25)
    GUICtrlSetFont(-1, 11)
    Local $idBut = GUICtrlCreateButton("Done", 200, 10, 80, 25)
    GUICtrlCreateLabel('Characters allowed:- ' & @CRLF & _
            '"a - z" upper and lower case;' & @CRLF & _
            '"0 - 9" numbers; and,' & @CRLF & _
            'One underscore "_", one minus sign, "-", or' & @CRLF & _
            'one space, " ", but only after any letter\s or digit\s.', 10, 40, 280, 100)
    GUICtrlSetFont(-1, 9)
    GUISetState(@SW_SHOW, $hFormatedInput)

    While 1
        Switch (GUIGetMsg())
            Case $GUI_EVENT_CLOSE, $idBut
                $iReply = MsgBox(33, "Check Input", "Characters input:- " & @CRLF & @CRLF & _ ;  1 OK (1) and Cancel (2) + 32 Question-mark icon
                        GUICtrlRead($idInput) & @CRLF & @CRLF & _
                        'Press "OK" to accept input data, or;' & @CRLF & _
                        'Press "Cancel" to continue to enter input data.')
                If $iReply = 1 Then ; OK (1) Pressed
                    ExitLoop ; Accept lesser length input.
                Else
                    ControlFocus($hFormatedInput, "", $idInput) ; Return focus to input
                EndIf
        EndSwitch
    WEnd
    $Res = GUICtrlRead($idInput)
    GUIDelete($hFormatedInput)
    Return $Res
EndFunc   ;==>_FormatedInput

; Ref to: https://www.autoitscript.com/forum/topic/180138-custom-input-mask/?do=findComment&comment=1293201
Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) ;  abcde 1234 f
    If $hWnd = $hFormatedInput And _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $idInput Then
        Local $s = StringRegExpReplace(GUICtrlRead($idInput), "(?i)[^\w\-\h]", "") ; Remove all characters that ae not in square brackets.
        $s = StringRegExpReplace($s, "(?i)(\A|[a-z0-9][\-_\h])[\-_\h]+", "\1") ;  Allow only one space, " ", "-", or "_" character only after any letter\s or digit\s.
        GUICtrlSetData($idInput, $s)
    EndIf
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>_WM_COMMAND

 

Link to comment
Share on other sites

Thanks Malkey,

with your additions , this is what I was wanting to get

#include <GUIConstants.au3>
#include <WinApi.au3>
#include <String.au3>

GUIRegisterMsg($WM_COMMAND, _WM_COMMAND)

Global $sStr = _FormatedInput() ; Get formated data from input control.
MsgBox(0, "Returned Results", $sStr)

Func _FormatedInput()
    Local $Res
    Global $hFormatedInput = GUICreate("Example", 300, 150)
    Global $idInput = GUICtrlCreateInput("", 10, 10, 180, 25)
    GUICtrlSetFont(-1, 11)
    Local $idBut = GUICtrlCreateButton("Done", 200, 10, 80, 25)
    GUICtrlCreateLabel('Characters allowed:- ' & @CRLF & _
            '"a - z" upper and lower case;' & @CRLF & _
            '"0 - 9" numbers; and,' & @CRLF & _
            'One underscore "_", one minus sign, "-", or' & @CRLF & _
            'one space, " ", but only after any letter\s or digit\s.', 10, 40, 280, 100)
    GUICtrlSetFont(-1, 9)
    GUISetState(@SW_SHOW, $hFormatedInput)

    While 1
        Switch (GUIGetMsg())
            Case $GUI_EVENT_CLOSE, $idBut
                $iReply = MsgBox(33, "Check Input", "Characters input:- " & @CRLF & @CRLF & _ ;  1 OK (1) and Cancel (2) + 32 Question-mark icon
                        GUICtrlRead($idInput) & @CRLF & @CRLF & _
                        'Press "OK" to accept input data, or;' & @CRLF & _
                        'Press "Cancel" to continue to enter input data.')
                If $iReply = 1 Then ; OK (1) Pressed
                    ExitLoop ; Accept lesser length input.
                Else
                    ControlFocus($hFormatedInput, "", $idInput) ; Return focus to input
                EndIf
        EndSwitch
    WEnd
    $Res = GUICtrlRead($idInput)
    GUIDelete($hFormatedInput)
    Return $Res
EndFunc   ;==>_FormatedInput

Func _AutoComplete()
    If StringLen(GUICtrlRead($idInput)) > 18 Then
        GUICtrlSetData($idInput, StringTrimRight(GUICtrlRead($idInput), 1))
        Return MsgBox(0, '', 'too long')
    EndIf
    Local $s = GUICtrlSetData($idInput, StringRegExpReplace(GUICtrlRead($idInput), "(?i)(\A|[\-_\h])[\-_\h]+", "\1"))
    $s = GUICtrlRead($idInput)
    If Not StringRegExp($s, '(?i)^[a-z\d\-\_\s]*$') Then
        GUICtrlSetData($idInput, StringTrimRight($s, 1))
        Return MsgBox(0, '', "Only English letters and numbers are allowed")
    EndIf
    GUICtrlSetData($idInput, _StringProper(StringReplace(GUICtrlRead($idInput), " ", "-")))
EndFunc   ;==>_AutoComplete

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) ;  abcde 1234 f
    If $hWnd = $hFormatedInput And _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $idInput Then _AutoComplete()
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>_WM_COMMAND

 

Edited by Deye
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...