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

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Bijay Kumar Sha ,

                                            You can achieve this by writing regex in onChange client script check below script for example make changes as per your requirement.

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

    var groupEmail = g_form.getValue('group_email');

    // Regular expression to validate DL email addresses
    var dlRegex = /^DL_[A-Za-z0-9]+$/;

    // Check if the entered email matches the DL regex pattern
    if (!dlRegex.match(groupEmail)) {
        // If the input is not a valid DL, show an error message and clear the field value
        alert('Please enter a valid Distribution List (DL). Individual email addresses are not allowed.');
        g_form.setValue('group_email', ''); // Clear the field value
    }
}

Kindly mark correct and helpful if applicable 

Hi @Chetan Mahajan ,

Thank you for your response. However, It's not working for me. Even if I'm putting a valid email of a DL, then alert is popping up. 

I'm providing you a few of valid Email IDs of DLs:

1. KID-WorldcheckOne-TechSupport@kid.com

2. KID-IncidentManagement-TISCEMGMT@kid.com

3. KID-GlobalEngineeringCapability@kid.com

4. KID-TISDataServicesOracle@kid.com

5. KID-SolutionsOfficeAWSAccountOwners@kid.com

 

Can you please let me know what changes needs to be done in the above code?

SANDEEP28
Mega Sage

@Bijay Kumar Sha What is the identifier(Common keyword) in DL email IDs?  is DL email ID is always going to start with "KID"?

@SANDEEP28  Yes. Correct. 'KID-' or 'kid-'.