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

Perfect! It is working as expected now.

Thanks for your indetail explanation @Harish KM 

Onkar Pandav
Tera Guru

something like this.

onChange client script:

 

var ga = new GlideAjax('EmailValidation');
ga.addParam('sysparm_name', 'checkEmail');
ga.addParam('sysparm_email_id', newValue);
ga.getXML(emailValue);

function emailValue(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
	if (answer == 'true') {
	        alert('This email is already exists. Please enter another email id');
                g_form.clearValue('<enter email field backend name>');
	}
}

 

 

Client callable script include:

 

 

checkEmail:function() {
        var getTask = new GlideRecord('sys_user');
        getTask.addQuery('email', this.getParameter('sysparm_email_id'));
        getTask.query();
        if (getTask.next()) {
            return true;
        } else
            return false;
},

 

Harsh Vardhan
Giga Patron

Hi @Archana23  Can you try with this script.

 

Catalog Client script: 

 

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

    //Type appropriate comment here, and begin script below

    var ga = new GlideAjax('validateEmail');
    ga.addParam('sysparm_name', 'email_validation');
    ga.addParam('sysparm_email', newValue);
    ga.getXML(emailVal);

    function emailVal(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
		
        if (answer == 'true') {
            alert('Email Already Exist ! ');
            g_form.clearValue('email');
        }


    }

}

 

CL script.JPG

 

Script Include : 

 

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

    email_validation: function() {

        var mail = this.getParameter('sysparm_email');
        var grm = new GlideRecord('sys_user');
        grm.addQuery('email', mail);
        grm.query();
        if (grm.hasNext())
            return 'true';
        else
            return 'false';

    },

    type: 'validateEmail'
});

 

SI email.JPG

 

 

Hope it will help you.

 

Thanks,

Harsh