How do you stop catalog item from submitting until client script conditions are met

Khalnayak
Tera Guru

Hi Guys,

I have a email address field within a Catalog item.

I have created a an onChange catalog client script which checks if the email field is valid syntax and if not throws an error message at the top of the form.

Now that all works fine, but I want to either make the submit button greyed out / unavailable or the field to be kept mandatory until the client script conditions are met, 

Because as it stands, users can enter an invalid email address syntax and still submit it!

Below is my code:

find_real_file.png

 

Many thanks,

Usmaan.

1 ACCEPTED SOLUTION

Hi,

Try onSubmit client script.

and add return false; in if condition after error message.

Thanks,

Dhananjay.

View solution in original post

8 REPLIES 8

Hi Usmaan,

 

As rightly suggested by others you need to use onSubmit instead of onChange()

Make sure you add return false; to the script to abort action.

Changing the Type to onSubmit will not work directly. You need to ensure that the onSubmit() function is used in client script.

In short, do below.

1. Change the Type to onSubmit

2. Replace first 5 lines of the script with below.

function onSubmit() {

3. Comment out last two '}'

Khalnayak
Tera Guru

Thanks all,

Changing to onSubmit has worked.

Below is the revised code:

 

function onSubmit() {


// This checks the email to ensure the names are seperated with a _ and the domain name is an exact match
var validRegExp = /^[a-zA-Z0-9]+(_)+[a-zA-Z0-9]+@(freshfields)\.com$/i;

var strEmail = g_form.getValue('team_email_address');


if (strEmail.search(validRegExp) == -1)
{

// This displays an error message at the top of the form in red
g_form.addErrorMessage('A valid e-mail address is required eg:location_name@freshfields.com.');
return false;
}


}

 

Thank you all for your help.

Regards.

Usmaan.

Hi Usmaan,

Glad that you got the approach.

Did you mistakenly mark other answer as correct?

As I see same comments were provided before by other community members Dhananjay and myself.

You can only have one response marked as Correct.

Please consider marking appropriate reply as Correct & Helpful.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I believe the changes you have made is firstly provided/suggested by me, so I will appreciate if you mark my response as correct.

Thanks,

Dhananjay.