Product: PowerShell Universal
Version: 3.9.7
I have a quite large form with some textboxes. I would like to validate each textbox, one by one with i.e. regex. What is the prefered way to do this. If I use the -OnValidate on the form the error message show up at the bottom which is quite uggly. The way I prefer it is by doing -OnValidate on each textbox but how do i combine this with -OnValidate on the form so the users are unable to submit the form if one or more fields are not valid.
Below is some example code. The code work as i wan´t i but I have to do the validation twice. Is there i better way? Like a variable that tells if the form is validated without any errors?
$Pages += New-UDPage -Name ‘Test validation’ -Content {
New-UDRow -Columns {
New-UDGrid -LargeSize 4 -Children {}
New-UDGrid -LargeSize 2 -Children {
New-UDForm -Id “form1” -Content {
New-UDTextbox -Id “Name” -Label “Name” -FullWidth -OnValidate {
If ($EventData -cmatch “[1]{4,50}$”)
{
New-UDFormValidationResult -Valid
}
Else
{
New-UDFormValidationResult -ValidationError 'Invalid text'
}
}
} -OnSubmit {
Show-UDToast $EventData.Name -Position center
} -ButtonVariant contained -OnValidate {
If ($EventData.Name -cmatch "^[a-z]{4,50}$")
{
New-UDFormValidationResult -Valid
}
Else
{
New-UDFormValidationResult -ValidationError ''
}
}
}
}
}
a-z ↩︎