need to have a validation check for that field, if there is already email available in the user tabl

Archana23
Tera Contributor

I have a single line text field called "email address", need to have a validation check for that field, if there is already email available in the user table which is same as the end user entered, we should throw the error saying that this email address is already exist,please choose different one.

Please help me in script part for this.

1 ACCEPTED SOLUTION

Hi @Archana23 sorry while copying client script i missed a line where there is a error in your client script your not comparing answer is true which is failing . Corrected below check bolded line which is missing in your code

 

function response(answer) {
        alert(answer);
       if(answer == 'true')
 {  g_form.setValue('email_of_the_shared_mailbox', '');
            g_form.showFieldMsg('email_of_the_shared_mailbox', "EMAIL ALREADY EXIST",'error');
    }
 
}
}
Regards
Harish

View solution in original post

17 REPLIES 17

Hi Harish,

 

Thanks for sharing the code, it is working as expected but whenever we enter the email address which is not in the user table , at that time also it is displaying the error.

Could you please check that.

Hi @Archana23 could you please share your script include and client script code here?

Regards
Harish

Script inlcude:
 
 Archana23_1-1710506008718.png

 

var SE_Validationcheckforemail = Class.create();
SE_Validationcheckforemail.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    type: 'SE_Validationcheckforemail',

    validateEmail:function(){
var mail = this.getParameter('sysparm_email'); // get email value from client script
gs.info("email"+mail);
var email = new GlideRecord('sys_user');
email.addQuery('email',mail);
email.query();
if(email.hasNext())
{
    return "This email is already exists. Please enter another email id, ";
}

},
});
 
 
Client script: 
Archana23_0-1710505971096.png
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var email = g_form.getValue('email');
    //alert(email);
    var ga = new GlideAjax('SE_Validationcheckforemail'); // script include name
    ga.addParam('sysparm_name', 'validateEmail'); // script include function name
    ga.addParam('sysparm_email', email); // pass value to script include
    ga.getXMLAnswer(response);

    function response(answer) {
        alert(answer);
        if (answer) {
            g_form.setValue('email', '');
            g_form.showFieldMsg('email', answer);

        }
    }
   //Type appropriate comment here, and begin script below
   
}

Hi @Archana23 can you Replace the if(email.hasNext())

{

return " email already exist";

}

 

To 

return email.hasNext() ? 'true' : 'false';

 and in client script

alert(answer);
        if (answer == 'true') {
g_form.setValue('email''');
            g_form.showFieldMsg('email', answer);


Regards
Harish

After replacing the code as above, it is showing the alert to true, and it is not displaying the alert like this email is already exist and if the email is not avialble in the user table also, it is not showing any error , it is showing the alert to true in both the cases.