Validation on Email field

Bijay Kumar Sha
Giga Guru

I've a CI form where there is a field called Group Email. I wanted to have a validation where this field should accept only valid DL no other text nor individual email id. It should only accept valid DL. 

How to achieve this?

1 ACCEPTED SOLUTION

@Bijay Kumar Sha For this requirement you need t o create client callable script include to get all the group email ID's and compare it with group email from CI form. Then write glideAjax logic to call that script include in OnChange client script.

 

Script Include - 

Name - getGroupEmailIDs

Client callable = Checked

Put below script  in script section - 

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

    getEmailList: function() {

        var groupEmailList = [];
        var matchFound = '';
        var groupEmail = this.getParameter('sysparm_groupEmail');
        var grGroup = new GlideRecord('sys_user_group');
        grGroup.addActiveQuery();
        grGroup.query();
        while (grGroup.next()) {
            if (grGroup.email)
                groupEmailList.push(grGroup.email.toString());
        }

        if (groupEmailList.length > 0) {
            for (var i = 0; i < groupEmailList.length; i++) {
                if (groupEmailList[i].toString().toUpperCase() == groupEmail.toUpperCase())
                    matchFound = 'Yes';
            }
        }
		
        if (!matchFound)
            matchFound = 'No';

		return matchFound;
    },

    type: 'getGroupEmailIDs'
});

 

onChange client script -

 

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

    var getGroupEmailList = new GlideAjax('global.getGroupEmailIDs');
    getGroupEmailList.addParam('sysparm_name', 'getEmailList');
    getGroupEmailList.addParam('sysparm_groupEmail', newValue)

    getGroupEmailList.getXMLAnswer(function(output) {
        if (output == 'No') {
            g_form.setValue('u_group_email', '');
            g_form.showFieldMsg('u_group_email', 'Please enter a valid DL email address', 'error');

        } else {
            g_form.clearMessages('u_group_email');
        }
    });

}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

View solution in original post

13 REPLIES 13

@Bijay Kumar Sha Use below code in onChange client script on "Group email" field. 

Replace "u_group_email"  with your group email field 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var regExp = /^(KID|kid){1}([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
	if (!regExp.test(newValue)) {
        
		g_form.setValue('u_group_email', '');
		g_form.showFieldMsg('u_group_email', 'Please enter a valid DL email address', 'error');
    }
    else{
		g_form.clearMessages('u_group_email');
	}
}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

Hi @SANDEEP28 , 

Thank you for your response. Yes, it works but not fully... Let me explain you as below:

When I put a valid DL - KID-SPINEProductTeam@kid.com and it’s recognizing, which is cool, I put in my personal email box it’s eliminating which is good, also I’m seeing it’s eliminating random characters as well, that’s all good, but when I altered the first DL a little bit to KID-ProductTeam@kid.com instead of KID-SPINEProductTeam@kid.com, then it still accepted it as a valid DL, but this is not a real DL this is just something I made up on the fly.

 

How to validate the correct DL ?

 

@Bijay Kumar Sha Do you want exact match to DL ? Are these DL's stored anywhere in ServiceNow table ? 

 

@SANDEEP28  In general, whenever you open a group, there it has mentioned as group email as per below screenshot.

BijayKumarSha_0-1689842035667.png

 

So, you can say it it sys_user_group.email

 

Yes, the validation should be exact match. I mean it should validate all the email IDs of all the DLs that are available in the instance. 

 

Because, let's say some one put 'KID-ProductTeam@kid.com' email id instead of the actual email id 'KID-SPINEProductTeam@kid.com' and if the form accept it, then later if some one try to send any email communication to the mentioned email id i.e 'KID-ProductTeam@kid.com', then the email won't get delivered. As it's a invalid email id which doesn't exist in the system. 

@Bijay Kumar Sha For this requirement you need t o create client callable script include to get all the group email ID's and compare it with group email from CI form. Then write glideAjax logic to call that script include in OnChange client script.

 

Script Include - 

Name - getGroupEmailIDs

Client callable = Checked

Put below script  in script section - 

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

    getEmailList: function() {

        var groupEmailList = [];
        var matchFound = '';
        var groupEmail = this.getParameter('sysparm_groupEmail');
        var grGroup = new GlideRecord('sys_user_group');
        grGroup.addActiveQuery();
        grGroup.query();
        while (grGroup.next()) {
            if (grGroup.email)
                groupEmailList.push(grGroup.email.toString());
        }

        if (groupEmailList.length > 0) {
            for (var i = 0; i < groupEmailList.length; i++) {
                if (groupEmailList[i].toString().toUpperCase() == groupEmail.toUpperCase())
                    matchFound = 'Yes';
            }
        }
		
        if (!matchFound)
            matchFound = 'No';

		return matchFound;
    },

    type: 'getGroupEmailIDs'
});

 

onChange client script -

 

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

    var getGroupEmailList = new GlideAjax('global.getGroupEmailIDs');
    getGroupEmailList.addParam('sysparm_name', 'getEmailList');
    getGroupEmailList.addParam('sysparm_groupEmail', newValue)

    getGroupEmailList.getXMLAnswer(function(output) {
        if (output == 'No') {
            g_form.setValue('u_group_email', '');
            g_form.showFieldMsg('u_group_email', 'Please enter a valid DL email address', 'error');

        } else {
            g_form.clearMessages('u_group_email');
        }
    });

}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!