maxLength attribute for UDTextbox

I’ve been trying various methods to set the maxLength attribute of the input created by New-UDTextbox with no success, including its -Max parameter, or calling Set-UDElement / Invoke-UDJavaScript. If I set the attribute using browser dev tools, it works fine. I just can’t seem to get PSU to set that attribute for the life of me.


The input is in a Stepper, so I know I can simply use validation to enforce the length requirement, but I’d really prefer to have the input at a capped length as well for admittedly aesthetic reasons.

Has anyone successfully implemented this before?

Product: PowerShell Universal
Version: 4.2.12

I knocked this together quickly, so can probably be improved, something like this might work for you;

New-UDApp -Content {  
    New-UDDynamic -Id TextField -Content {
        New-UDTextBox -Id TextBoxField -Value $Session:TextBoxData -OnChange {
            $TextBoxInput =  ((Get-UDElement -Id TextBoxField).value) -replace "[^0-9]", ""
            if (($TextBoxInput).Length -lt 8) {               
                $SubStringLength = ($TextBoxInput).Length
            } else { 
                $SubStringLength = 8

            $Session:TextBoxData = $TextBoxInput.SubString(0, $SubStringLength)

            Sync-UDElement TextField

I appreciate the code snippet. I had tried something similar but honestly, I’ll just stick with the enforcement through stepper validation without the extra fuss, as that will also address input less than 7 digits long. Would really be nice to be able to set standard HTML attributes like maxLength on inputs, though.

You can achieve this by using the -mask parameter:

In your case this would be:

New-UDTextbox -Mask "00000000"

The problem? Apparently adam wants to get rid of this parameter which doesnt work with regex as stated in the docs… it only works well with pattern mask…

In addition I realized that if you use -mask the -onvalidate won’t work in conjunction anymore. So unfortunately you can’t have both things together (at least at the moment).

I really hope that -mask will not disapper because based on some use cases I like this one better…

This has been implemented in 4.2.16, according to its release notes.

Added -MaximumLength to -New-UDTextbox (#3239)

1 Like

Yep! I opened an enhancement request for it! :innocent: