Jump to content

Custom formating text in inputbox possible?


n3wbie
 Share

Recommended Posts

Can we custom format input box ?

Like if i have a telphone number as 123456789

Can it be validated as 123-456-7890

Actually i want to do it as alpha numeric

 

Like first 5 characters as alphabets than a space than next 4 as numbers than again space and last again a single letter?

Eg 

Input abcde1234f to become abced 1233 f

I know i can do it in backend reading whole string and formating it 

But i want to block input something similar to $esnumber

Edited by n3wbie
Added some extra information for better understamding
Link to comment
Share on other sites

I don't think there's anything like $ESNUMBER For this, you could use a regexp to figure out if the input matches what you want then set the input to the correct format

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

Global $hMain = GUICreate("Example", 200, 40)
Global $inpInput = GUICtrlCreateInput("", 10, 10, 180, 20)

GUIRegisterMsg($WM_COMMAND, WM_COMMAND)

GUISetState(@SW_SHOW, $hMain)

While (True)
    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            Exit 0
    EndSwitch
WEnd


Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    Local $aRegExp

    Switch (_WinAPI_HiWord($wParam))
        Case $EN_CHANGE
            Switch (_WinAPI_LoWord($wParam))
                Case $inpInput
                    $aRegExp = StringRegExp(GUICtrlRead($inpInput), "([a-zA-Z]{5})([0-9]{4})([a-zA-Z]{1})", 3)
                    If (Not @error) Then GUICtrlSetData($inpInput, _ArrayToString($aRegExp, "-"))
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

 

Link to comment
Share on other sites

  • 2 weeks later...

This example  auto-formats the input data to a particular sequence of characters.
For example 5 lowercase letters,
followed by 4 digits,
followed by 1 lowercase letter,
like "abcde1234f ", becomes "abcde 1234 f ".

#include <GUIConstants.au3>
#include <WinApi.au3>
#include <Array.au3>
; https://www.autoitscript.com/forum/topic/187551-custom-formating-text-in-inputbox-possible/

Global $hMain = GUICreate("Example", 300, 150)
GUISetFont(10)
Global $inpInput = GUICtrlCreateInput("", 10, 10, 180, 20)
GUICtrlCreateLabel('Format: "aaaaa dddd a" (without added spaces)' & @CRLF & _
        "Where:-" & @CRLF & @TAB & _
        '"a" is a lowercase letter, (a-z), and;' & @CRLF & @TAB & _
        '"d" is a digit (0-9).', 10, 35, 280, 70)
GUICtrlSetFont(-1, 9)


GUIRegisterMsg($WM_COMMAND, _WM_COMMAND)

GUISetState(@SW_SHOW, $hMain)

While (True)
    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) ;  abced 1233 f
    If BitAND($wParam, 0x0000FFFF) = $inpInput Then
        Local $s = StringRegExpReplace(GUICtrlRead($inpInput), "[^\p{Ll}0-9]", "") ; Remove all characters that ae not lowercase letters nor digits.
        Switch StringLen($s)
            Case 0 To 5
                $s = StringRegExpReplace($s, "[^\p{Ll}]", "")
                GUICtrlSetData($inpInput, $s)
            Case 6 To 9
                $s = StringLeft($s, 5) & StringRegExpReplace(StringTrimLeft($s, 5), "[^0-9]", "") ;Remove all characters that ae not  digits.
                GUICtrlSetData($inpInput, StringLeft($s, 5) & " " & StringMid($s, 6))
            Case 10 To 12
                $s = StringLeft($s, 9) & StringRegExpReplace(StringTrimLeft($s, 9), "[^\p{Ll}]", "") ; Remove all characters that ae not lowercase letters.
                GUICtrlSetData($inpInput, StringLeft($s, 5) & " " & StringMid($s, 6, 4) & " " & StringMid($s, 10, 1))
        EndSwitch
    EndIf
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>_WM_COMMAND

And here is an updated version.

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

GUIRegisterMsg($WM_COMMAND, _WM_COMMAND)

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


Func _FormatedInput()
    local $Res
    Global $hFormatedInput = GUICreate("Example", 300, 150)
    Global $inpInput = GUICtrlCreateInput("", 10, 10, 180, 25)
    GUICtrlSetFont(-1, 11)
    Local $idBut = GUICtrlCreateButton("Done", 200, 10, 80, 25)
    GUICtrlCreateLabel('Format: "aaaaa dddd a" (without added spaces)' & @CRLF & _
            "Where:-" & @CRLF & @TAB & _
            '"a" is a lowercase letter, (a-z), and;' & @CRLF & @TAB & _
            '"d" is a digit (0-9).', 10, 40, 280, 70)
    GUICtrlSetFont(-1, 9)
    GUISetState(@SW_SHOW, $hFormatedInput)

    While 1
        Switch (GUIGetMsg())
            Case $GUI_EVENT_CLOSE, $idBut
                $iLen = StringLen(StringRegExpReplace(GUICtrlRead($inpInput), "[^\p{Ll}0-9]", "")) ; Remove all characters that ae not lowercase letters nor digits.
                ConsoleWrite($iLen & @CRLF)
                If $iLen < 10 Then
                    $iReply = MsgBox(33, "Error", "Only " & $iLen & " characters entered !!" & @CRLF & _ ;  1 OK (1) and Cancel (2) + 32 Question-mark icon
                            'Press "OK" to accept input data as is, 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, "", $inpInput) ; Return focus to input
                    EndIf
                Else
                    ExitLoop
                EndIf
        EndSwitch
    WEnd
    $Res =  GUICtrlRead($inpInput)
    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) = $inpInput Then
        Local $s = StringRegExpReplace(GUICtrlRead($inpInput), "[^\p{Ll}0-9]", "") ; Remove all characters that ae not lowercase letters nor digits.
        Switch StringLen($s)
            Case 1 To 5
                $s = StringRegExpReplace($s, "[^\p{Ll}]", "")
                GUICtrlSetData($inpInput, $s)
                ;ConsoleWrite(StringLen($s) & "  0-6  " & $s & @CRLF)
            Case 6 To 9
                $s = StringLeft($s, 5) & StringRegExpReplace(StringTrimLeft($s, 5), "[^0-9]", "") ;Remove all characters that ae not  digits.
                GUICtrlSetData($inpInput, StringLeft($s, 5) & " " & StringMid($s, 6))
                ;ConsoleWrite(StringLen($s) & "  6-9  " & $s & @CRLF)
            Case 10 To 11
                $s = StringLeft($s, 9) & StringRegExpReplace(StringTrimLeft($s, 9), "[^\p{Ll}]", "") ; Remove all characters that ae not lowercase letters.
                GUICtrlSetData($inpInput, StringLeft($s, 5) & " " & StringMid($s, 6, 4) & " " & StringMid($s, 10, 1))
                ;ConsoleWrite(StringLen($s) & "  10-11  " & $s & @CRLF)
        EndSwitch
    EndIf
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>_WM_COMMAND

 

Edited by Malkey
Link to comment
Share on other sites

  • 2 weeks later...
On 4/6/2017 at 4:42 AM, Malkey said:

This example  auto-formats the input data to a particular sequence of characters.
For example 5 lowercase letters,
followed by 4 digits,
followed by 1 lowercase letter,
like "abcde1234f ", becomes "abcde 1234 f ".

#include <GUIConstants.au3>
#include <WinApi.au3>
#include <Array.au3>
; https://www.autoitscript.com/forum/topic/187551-custom-formating-text-in-inputbox-possible/

Global $hMain = GUICreate("Example", 300, 150)
GUISetFont(10)
Global $inpInput = GUICtrlCreateInput("", 10, 10, 180, 20)
GUICtrlCreateLabel('Format: "aaaaa dddd a" (without added spaces)' & @CRLF & _
        "Where:-" & @CRLF & @TAB & _
        '"a" is a lowercase letter, (a-z), and;' & @CRLF & @TAB & _
        '"d" is a digit (0-9).', 10, 35, 280, 70)
GUICtrlSetFont(-1, 9)


GUIRegisterMsg($WM_COMMAND, _WM_COMMAND)

GUISetState(@SW_SHOW, $hMain)

While (True)
    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) ;  abced 1233 f
    If BitAND($wParam, 0x0000FFFF) = $inpInput Then
        Local $s = StringRegExpReplace(GUICtrlRead($inpInput), "[^\p{Ll}0-9]", "") ; Remove all characters that ae not lowercase letters nor digits.
        Switch StringLen($s)
            Case 0 To 5
                $s = StringRegExpReplace($s, "[^\p{Ll}]", "")
                GUICtrlSetData($inpInput, $s)
            Case 6 To 9
                $s = StringLeft($s, 5) & StringRegExpReplace(StringTrimLeft($s, 5), "[^0-9]", "") ;Remove all characters that ae not  digits.
                GUICtrlSetData($inpInput, StringLeft($s, 5) & " " & StringMid($s, 6))
            Case 10 To 12
                $s = StringLeft($s, 9) & StringRegExpReplace(StringTrimLeft($s, 9), "[^\p{Ll}]", "") ; Remove all characters that ae not lowercase letters.
                GUICtrlSetData($inpInput, StringLeft($s, 5) & " " & StringMid($s, 6, 4) & " " & StringMid($s, 10, 1))
        EndSwitch
    EndIf
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>_WM_COMMAND

And here is an updated version.

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

GUIRegisterMsg($WM_COMMAND, _WM_COMMAND)

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


Func _FormatedInput()
    local $Res
    Global $hFormatedInput = GUICreate("Example", 300, 150)
    Global $inpInput = GUICtrlCreateInput("", 10, 10, 180, 25)
    GUICtrlSetFont(-1, 11)
    Local $idBut = GUICtrlCreateButton("Done", 200, 10, 80, 25)
    GUICtrlCreateLabel('Format: "aaaaa dddd a" (without added spaces)' & @CRLF & _
            "Where:-" & @CRLF & @TAB & _
            '"a" is a lowercase letter, (a-z), and;' & @CRLF & @TAB & _
            '"d" is a digit (0-9).', 10, 40, 280, 70)
    GUICtrlSetFont(-1, 9)
    GUISetState(@SW_SHOW, $hFormatedInput)

    While 1
        Switch (GUIGetMsg())
            Case $GUI_EVENT_CLOSE, $idBut
                $iLen = StringLen(StringRegExpReplace(GUICtrlRead($inpInput), "[^\p{Ll}0-9]", "")) ; Remove all characters that ae not lowercase letters nor digits.
                ConsoleWrite($iLen & @CRLF)
                If $iLen < 10 Then
                    $iReply = MsgBox(33, "Error", "Only " & $iLen & " characters entered !!" & @CRLF & _ ;  1 OK (1) and Cancel (2) + 32 Question-mark icon
                            'Press "OK" to accept input data as is, 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, "", $inpInput) ; Return focus to input
                    EndIf
                Else
                    ExitLoop
                EndIf
        EndSwitch
    WEnd
    $Res =  GUICtrlRead($inpInput)
    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) = $inpInput Then
        Local $s = StringRegExpReplace(GUICtrlRead($inpInput), "[^\p{Ll}0-9]", "") ; Remove all characters that ae not lowercase letters nor digits.
        Switch StringLen($s)
            Case 1 To 5
                $s = StringRegExpReplace($s, "[^\p{Ll}]", "")
                GUICtrlSetData($inpInput, $s)
                ;ConsoleWrite(StringLen($s) & "  0-6  " & $s & @CRLF)
            Case 6 To 9
                $s = StringLeft($s, 5) & StringRegExpReplace(StringTrimLeft($s, 5), "[^0-9]", "") ;Remove all characters that ae not  digits.
                GUICtrlSetData($inpInput, StringLeft($s, 5) & " " & StringMid($s, 6))
                ;ConsoleWrite(StringLen($s) & "  6-9  " & $s & @CRLF)
            Case 10 To 11
                $s = StringLeft($s, 9) & StringRegExpReplace(StringTrimLeft($s, 9), "[^\p{Ll}]", "") ; Remove all characters that ae not lowercase letters.
                GUICtrlSetData($inpInput, StringLeft($s, 5) & " " & StringMid($s, 6, 4) & " " & StringMid($s, 10, 1))
                ;ConsoleWrite(StringLen($s) & "  10-11  " & $s & @CRLF)
        EndSwitch
    EndIf
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>_WM_COMMAND

 

thanks bro will give it a try

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