Verify banned words in catalog item variable(has regex) by storing banned words in custom table

Community Alums
Not applicable

Hi Everyone,

 

I am having requirement, where I have to validate banned words in variable (dl_name), banned words have stored in custom table name as ‘Banned words’ under ‘word’ column. And the variable has ‘Validation Regex’ i.e. ^DL_.*@snowmail.com$. 

Whenever in variable (dl_name) if user keep any word and if that word is listed in ‘Banned words’ custom table ‘word’ column then it should throw error message as ‘This word can not be used in distribution DL address’ and will not allow user to submit request. 

Ex. Banned words could be Horse, Tiger, Dog etc

dl_name = DL_Horse@snowmail.com     In that case it should not allow to submit and should throw error message.

 

 

I tried using onChange client script Glide ajax script include but In portal getting browser console error. If anyone came across any similar type of requirement please suggest me solution.

 

Thank you in advance!!!

 

 

1 ACCEPTED SOLUTION

Hey, Your script include can't have an initialise function (unless you also invoke the AbstractAjaxProcessor

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

	checkBannedWords: function(){
		var inputName = String(this.getParameter('sysparm_dlName')).toLowerCase();
		if(!inputName)
			return 'NO_VALUE';
		
		var bannedWordsGR = new GlideRecord('u_banned_words');
		bannedWordsGR.query();
		while(bannedWordsGR.next()){
			var word = String(bannedWordsGR.getValue('u_word')).toLowerCase();

			if(inputName.contains(word))
				return true;
		}

		return false;
	},

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

    var dlName = newValue.toLowerCase(); // Convert to lowercase for case-insensitive comparison

    var ga = new GlideAjax('BannedWordsChecker');
    ga.addParam('sysparm_name', 'checkBannedWords');
    ga.addParam('sysparm_dlName', dlName);
    ga.getXMLAnswer(function(response) {

        if (response == null)
            console.error('Invalid Response');

        if (response == 'true')
            //Bad word foudn

            if (response == 'false')
                return;
    });
}

View solution in original post

5 REPLIES 5

Kieran Anson
Kilo Patron

Hi,

Can you share your GlideAjax client script and script include so we can advise on how to amend it

Community Alums
Not applicable

Script include:

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

initialize: function() {},

checkBannedWords: function() {
var dlName = this.getParameter('sysparm_dlName').toLowerCase();
var gr = new GlideRecord('u_banned_words');
gr.query();
var bannedWords = [];

while (gr.next()) {
bannedWords.push(gr.getValue('u_word').toLowerCase()); // Ensure banned words are in lowercase
}

for (var i = 0; i < bannedWords.length; i++) {
if (dlName.includes(bannedWords[i])) {
gs.info('Checking banned words for: ' + dlName);
return 'true';
}
}
return 'false';
},

type: 'BannedWordsChecker'
});
--------------------------------------------------------------------------------------
onChange on dl_name:

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

var dlName = newValue.toLowerCase(); // Convert to lowercase for case-insensitive comparison

var ga = new GlideAjax('BannedWordsChecker');
ga.addParam('sysparm_name', 'checkBannedWords');
ga.addParam('sysparm_dlName', dlName);
ga.getXMLAnswer(function(response) {
var result = response.responseXML.documentElement.getAttribute('answer');

if (result === 'true') {
alert('Distribution List Address contains a word that is banned');
g_form.setValue('dl_name', oldValue); // Revert to old value
} else {
alert('Distribution List Address is valid');
}
});
}

-----------------------------------------------------------------------

I am getting error in portal:

ErrorThere is a JavaScript error in your browser console


getMessage (key="Quantity {0}"): synchronous use not supported in Mobile or Service Portal unless message is already cached
Unhandled exception in GlideAjax.




Hey, Your script include can't have an initialise function (unless you also invoke the AbstractAjaxProcessor

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

	checkBannedWords: function(){
		var inputName = String(this.getParameter('sysparm_dlName')).toLowerCase();
		if(!inputName)
			return 'NO_VALUE';
		
		var bannedWordsGR = new GlideRecord('u_banned_words');
		bannedWordsGR.query();
		while(bannedWordsGR.next()){
			var word = String(bannedWordsGR.getValue('u_word')).toLowerCase();

			if(inputName.contains(word))
				return true;
		}

		return false;
	},

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

    var dlName = newValue.toLowerCase(); // Convert to lowercase for case-insensitive comparison

    var ga = new GlideAjax('BannedWordsChecker');
    ga.addParam('sysparm_name', 'checkBannedWords');
    ga.addParam('sysparm_dlName', dlName);
    ga.getXMLAnswer(function(response) {

        if (response == null)
            console.error('Invalid Response');

        if (response == 'true')
            //Bad word foudn

            if (response == 'false')
                return;
    });
}

Community Alums
Not applicable

Hi @Community Alums ,
Can you please share the client script and Script Include scripts so that we can take a look and answer your query.

Meanwhile I have created a script based on your inputs that you can try, you need to update it based on the table and fields that you have configured.

 

Script Include

var wordsCheck = Class.create();
wordsCheck.prototype = {
    initialize: function() {},

    checkwords: function(input) {
        var words = [];
        var gr = new GlideRecord('u_banned_words'); // Change the table to your actual table name in your instance
        gr.query();
        while (gr.next()) {
            words.push(gr.getValue('u_word').toLowerCase());
        }

        for (var i = 0; i < words.length; i++) {
            if (input.toLowerCase().indexOf(words[i]) != -1) {
                return true; // Found a banned word
            }
        }
        return false;
    },

    type: 'wordsCheck'
};

 

 Client Script-

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
// your code for validation of the regex

    // call the above Script Include
    var ga = new GlideAjax('wordsCheck');
    ga.addParam('sysparm_name', 'checkwords');
    ga.addParam('sysparm_input', newValue);
    ga.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'true') {
            g_form.showFieldMsg('dl_name', 'This word cannot be used in DL address', 'error');
            g_form.clearValue('dl_name'); // Clearing the input to prevent form submission
        }
    });
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar