Validate Email Address in service catalog form

phil34
Tera Expert

Hi All,

 

I am sure someone has already done this, but I can't find a solution.

 

I have a catalogue item that is used to request an email address. The variable is "u_email_address"

When a user inputs an email address in that field I want to check if the email address already exists in the user table in our service now instance. This is what I want to happen when they are completing the form.

 

User types requested address in the field.

phil34_0-1689033535639.png

If the address entered by the user is already in the user table, then I want a message box to appear saying.

 

"The requested address is already in use"

 

When the user clicks on the OK button of the message box the text is cleared from the field and the user is required to enter another email address.

 

If they enter an email address that is NOT already in the user table then they can move to the next field.

 

I am new at scripting so I might need a bit of hand-holding if it is more complex. Hope you can help.

Cheers

Phil

 

phil34_2-1689034291794.png

 

 

1 ACCEPTED SOLUTION

Hey Phil,

 

You did almost right,

 

Use the below code in client script,

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
   }
    var usrGA = new GlideAjax('Validate_Email');
    usrGA.addParam('sysparm_name''validateEmail');
    usrGA.addParam('sysparm_email', newValue);
    usrGA.getXMLAnswer(_result);

    function _result(answer) {
        if (answer=='true') {
            alert('User Name already exists');
            g_form.clearValue('u_email_address');
        }
    }
}
Hope it helps and please mark helpful if it solves the issue.
Thanks,
Kiran
 

 

View solution in original post

6 REPLIES 6

Hi @phil34 ,

 

Please change your catalog client script to the following:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == "") {
        return;
    }
    var usrGA = new GlideAjax("Validate_Email");
    usrGA.addParam("sysparm_name", "validateEmail");
    usrGA.addParam("sysparm_email", newValue);
    usrGA.getXMLAnswer(_result);

    function _result(answer) {
        if (answer == '1') {
            alert("User Name already exists");
            g_form.clearValue("u_email_address");
        }
    }
}

The correction is in the if(answer == '1') 

This is because GlideAjax does not return true/false - it returns '1' for true and '0' for false.

Please let me know if this resolves your requirement, if yes, mark my answer as correct.


Please mark this post as a solution and also as helpful, if this resolves your issue or query.

Thanks,
Subhadeep Ghosh.

Hi Kilo, for whatever reason Kiran's solution worked. I did try both