onChange and onSubmit functions in the same Catalog Client Script

galvi024
Kilo Explorer

I have a Service Catalog item that needs a validation added to a Single Line Text field. We'd like to do the validation onChange as well as onSubmit. I know an easy way to do it would be to create two separate Catalog Client Scripts...one for onChange and one for onSubmit. But we'd like to keep this to one script.

Does anyone know if that is possible?

5 REPLIES 5

ssb
Tera Guru

I'd probably go with putting the validation into a Script Include and then calling the same function from two Client Scripts - then at least the code is all contained in one spot.

You might be able to set up an onChange script that also defines an onLoad function, but if it _did_ work, it'd be a hack 🙂


An onChange is an onLoad if you take out the (!isLoading).

I think loading the page is considered a change.


Jim Coyne
Kilo Patron

If the code is specific to the catalog item, you can actually define a custom function alongside an onLoad script. I typically have a "Setup Form" onLoad catalog client script for most of my catalog items that does any initialization or customizations and will include any shared custom functions here as well. So this is what the script field of the onLoad client script would look like:



function onLoad() {
//may or may not include any actual code here
}

function u_customFunction() {
//whatever you need to do goes in here...

}



Otherwise, as ssb says, you can create the custom code as a Script Include to be shared by multiple catalog items.


I am not too concerned with what happens onLoad, since this text box should not have any data in it until the user enters it.

I'd like to validate their input after they enter the data into the field(onChange). If the validation fails, we will post and alert to the user. However, I think I will still need to validate on submission(onSubmit) of the form as well, since failing the onChange doesn't force the user to fix the problem.

I could also skip the validation onChange and only do it onSubmit, but I personally find it annoying to get to the end of a form and find out that one field at the top needs to be redone.