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 @Archana23 I tested in PDI, it works fine for me

Script Include:

validateEmail:function(){
var mail = this.getParameter('sysparm_email');
gs.info("email"+mail);
var email = new GlideRecord('sys_user');
email.addQuery('email',mail);
email.query();
return email.hasNext() ? 'true' : 'false';

},
Client script:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var email = g_form.getValue('email');
    alert(email);
    var ga = new GlideAjax('demoUtils');
    ga.addParam('sysparm_name', 'validateEmail');
    ga.addParam('sysparm_email', email);
    ga.getXMLAnswer(response);

    function response(answer) {
        alert(answer);
       if(answer == 'true')
{
   g_form.setValue('email', '');
            g_form.showFieldMsg('email', "EMAIL ALREADY EXIST",'error');
    }
}
}
Result:
HarishKM_2-1710551197356.png

 


HarishKM_1-1710550934555.png

 

Regards
Harish

Hi Harish,

 

I tried the above script it is working for the existing email, it is showing the error and clearing the value.

But If I give the email which is not available in the user table, the alert is triggering to false from the browser but in this case also it is clearing the entered value and showing the field message 'EMAIL ALREADY EXIST'.

The below attached is the code I tried.
 
Script include
 
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();
return email.hasNext() ? 'true' : 'false';
 
},
});
 
 
Client script: 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
    var email = g_form.getValue('email_of_the_shared_mailbox');
    alert(email);
    var ga = new GlideAjax('SE_Validationcheckforemail');
    ga.addParam('sysparm_name', 'validateEmail');
    ga.addParam('sysparm_email', email);
    ga.getXMLAnswer(response);
 
    function response(answer) {
        alert(answer);
       
   g_form.setValue('email_of_the_shared_mailbox', '');
            g_form.showFieldMsg('email_of_the_shared_mailbox', "EMAIL ALREADY EXIST",'error');
    }
 
 
}
 

@Archana23  Have you tried with the script which i sent you ?

 

I can see you don't have if block in your client side scripting. 

Script include

 

 


 
var SE_Validationcheckforemail = Class.create();
SE_Validationcheckforemail.prototype = Object.extendsObject(AbstractAjaxProcessor, {    
 
validateEmail:function(){
var mail = this.getParameter('sysparm_email'); // get email value from client script

var email = new GlideRecord('sys_user');
email.addQuery('email',mail);
email.query();
if(email.hasNext())
return 'true';
else
return 'false'; 
},
type: 'SE_Validationcheckforemail',
});
 

 

 

Client script:

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
    var email = g_form.getValue('email_of_the_shared_mailbox');
    var ga = new GlideAjax('SE_Validationcheckforemail');
    ga.addParam('sysparm_name', 'validateEmail');
    ga.addParam('sysparm_email', email);
    ga.getXMLAnswer(response);
 
    function response(answer) {
       if (answer == 'true') {       
            g_form.setValue('email_of_the_shared_mailbox', '');
            g_form.showFieldMsg('email_of_the_shared_mailbox', "EMAIL ALREADY EXIST",'error');
    }
}
 
 
}

 

 

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