Email Validation using script include

rajibosss11
Tera Contributor

I have a problem in domain validation example (aa.com ,bb.com ,cc.com)
i have email field it should only allow above domain name  after @ but this requirement should get by script include 
Any one please support on this 

3 REPLIES 3

GlideFather
Tera Patron

Hi @rajibosss11,

 

if you want to achieve it by a script include then the script include must be called as it has no trigger.

 

Usually it can be called from a (catalog) client script..

 

Is this email validation in backend or portal form?

  • Backend > client script,
  • Portal > catalog client script,
  • or both > CS a CCS both needed in parallel.

Also, you might want to write an onChange (C)CS to inform user that only defined email is available and onSubmit not to allow them if the condition is not respected.

 

Perhaps this can be done by (C)CS without having the need of script include.

 

Or you might be able to do it by (Catalog) UI Policy as well.

 

Share what you have and what behaviour does it do and we can brainstorm/debug together

_____
No AI was used in the writing of this post. Pure #GlideFather only

vignesh parthib
Tera Guru

Hi @rajibosss11 

 

you create  client script and script include

  1. Create a Script Include that returns the list of allowed domains.

  2. Call that Script Include using GlideAjax from a Client Script.

  3. Validate the email field before saving.

Client script:

function onChange(control, oldValue, newValue) {
    if (!newValue) return;

    var ga = new GlideAjax('Script include name');
    ga.addParam('sysparm_name', 'getAllowedDomains ');
    ga.getXML(function (response) {
        var domains = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));

        // Extract domain from input email
        var emailDomain = newValue.split('@')[1];

        if (!domains.includes(emailDomain)) {
            g_form.showFieldMsg('Field Name', 
                "Invalid email domain. Allowed domains: " + domains.join(", "),
                "error");
            g_form.setValue('field  name', '');
        }
    });
}


Script include functon:

getAllowedDomains: function () {
        // Define allowed domains here
        var domains = ["aa.com", "bb.com", "cc.com"];
        return JSON.stringify(domains);
    },

 

Thanks,
Vignesh
"If this solution resolves your issue, kindly mark it as correct."

No its not works