Client Script OnChange as the user types

Michael278
Kilo Explorer

We are looking for a way to validate the text in a string field as the user is typing via regex. We are using an OnChange Client Script, but the function does not run until the field on the form loses focus. We're looking to trigger the OnChange event every time a user types a character into the field. Is there way to do this without forcing the script to run in a loop?

4 REPLIES 4

Mark Stanger
Giga Sage

If you're doing this on a standard form you can attach a keypress event to the field control.  Here is an example from SNGuru that I've used in the past.

https://www.servicenowguru.com/scripting/client-scripts-scripting/maximum-length-large-string-fields-servicenowcom/

Michael278
Kilo Explorer

Thank you for the quick reply. It doesn't appear that getControl works properly on a service portal form (returns null). Do you know of any similar options on a portal form?

Yeah, the forums are littered with posts from people complaining about the lack of 'getControl' in the Service Portal.  Unless you're working with a custom widget (which I'm guessing you aren't because you're using 'onChange' client scripts) this just isn't possible at the moment.  You're going to have to use the 'onChange' event instead.

Mark Stanger
Giga Sage

Just checking on on this one.  Has this question been answered or is there more information I can help you with?  If it's been answered, please mark the answer above as the correct one so people know this has been taken care of.  Thanks!