[quote=126798:@Norman Palardy]I’d do something more interactive & simple NOT let people type in characters that are not allowed
It makes for a better experience than typing something in, thinking its legal only to finally be told “thats not allowed because this character is not permitted” - so don’t permit the person to type it in
Filter right when you have the keydown event
The list is pretty short[/quote]
The way I do it is to replaceall in the Text property in the change event. That way it works for both keyboard and paste entries.
Kim’s option looks good to me but thanks.
[quote=126809:@Norman Palardy]Sure - but do it actively as things are typed / pasted instead of wait THEN validate it
Anyone still hate web sites that you fill in the form, hit “enter” (or submit or whatever) and THEN they say “You must fill in the XXXX field”
And then wipe all the dat a out so you can try again ?
That kind of block mode validation is infuriating - we can do better - and so we should[/quote]
Thanks for that. I have already written a class which sets the background to red with invalid input so it is very easy to tell if the input is invalid.
Whatever works for you. I would think it is better to simply not take into account invalid keys than to penalize the user with any signal, unless you really mean to educate him.
You could apply Kem’s method in the keydown event of the TextField as such :
[code]Function KeyDown(Key As String) As Boolean
static rx as RegEx
if rx is nil then
rx = new RegEx
rx.SearchPattern = “(?mi-Us)^[A-Z]$|^[_A-Z][_0-9A-Z]+$”
end if
if rx.Search( key ) is nil then
// Contains illegal characters
return true
end if
End Function
[/code]
I would use Kem’s second method in the TextField TextChange event in case of paste. Which could suffice without the test for len for all input without the keydown method.
[code]Sub TextChange()
static oldtext as string = me.text
if len(me.text)-len(oldtext) > 2 then
static rx as RegEx
if rx is nil then
rx = new RegEx
rx.SearchPattern = “[^0-9A-Z_]”
rx.ReplacementPattern = “”
rx.Options.ReplaceAllMatches = true
end if
me.text = rx.Replace( me.text )
end if
oldtext = me.text
End Sub
[/code]