Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Mandatory fields are accepting spaces, without having to enter any character for html type field.

harigundala
Tera Contributor

Hello,

I'm working on a requirement where user should not be able to enter empty spaces to a mandatory html type field, but should be able to give characters, special characters, number, images. I have 9 mandatory fields and I'm using a Reusable Validation Functions that is called 9 times. Now my issue is, its allowing characters, special characters, numbers but not the images. 

Here is the reusable function script I'm using to validate characters, special characters, numbers:

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

function onLoad() {
 
    window.validateField = function(fieldName) {
        var fieldValue = g_form.getValue(fieldName).trim();
        var textOnlyValue = fieldValue.replace(/<[^>]*>/g, '').trim();
        var fieldLabel = g_form.getLabel(fieldName).innerText || g_form.getLabel(fieldName).textContent;
        if (textOnlyValue === '' || textOnlyValue === ' ') {
            alert(fieldLabel + ' cannot be empty or contain only spaces.');
            g_form.setValue(fieldName, '');
            return false;
        }

        var regex = /^[a-zA-Z0-9\s.,!?'"\\/]*$/;
        if (!regex.test(textOnlyValue)) {
            alert(fieldLabel + ' cannot be empty. Please enter data into the field.');
            g_form.setValue(fieldName, '');
            return false;
        }

        var cleanedValue = fieldValue.replace(/\s+/g, ' ').trim();
        if (fieldValue !== cleanedValue) {
            g_form.setValue(fieldName, cleanedValue);
        }

        return true;
    };
}
-------------------------------------------------------------------------------------------------------------------------------
I have now modified this script to allow images or attachments and its not working :
-------------------------------------------------------------------------------------------------------------------------------
function onLoad() {
   
    window.validateField = function(fieldName) {
        var fieldValue = g_form.getValue(fieldName).trim();
        var textOnlyValue = fieldValue.replace(/<[^>]*>/g, '').trim();
        var fieldLabel = g_form.getLabel(fieldName).innerText || g_form.getLabel(fieldName).textContent;
        if (textOnlyValue === '' || textOnlyValue === ' ') {
            alert(fieldLabel + ' cannot be empty or contain only spaces.');
            g_form.setValue(fieldName, '');
            return false;
        }
        var regex = /^[a-zA-Z0-9\s.,!?'"\\/&*+_=-:;<>[\](){}|`~^$#%@]*$/;
        if (!regex.test(textOnlyValue)) {
            alert(fieldLabel + ' cannot be empty. Please enter data into the field.');
            g_form.setValue(fieldName, '');
            return false;
        }
        var htmlTagsAllowed = fieldValue.replace(/<[^>]+>/g, function(tag) {
            if (tag.toLowerCase().includes('<img')) {
                return tag;
            }
            return '';
        });

        if (htmlTagsAllowed !== fieldValue) {
            g_form.setValue(fieldName, htmlTagsAllowed);
        }
        var cleanedValue = fieldValue.replace(/\s+/g, ' ').trim();
        if (fieldValue !== cleanedValue) {
            g_form.setValue(fieldName, cleanedValue);
        }

        return true;
    };
}
----------------------------------------------------------------------------------------------------------------------------------
 Any guidance or support is much appreciated.
 
Thank you,
Hari
 

 

0 REPLIES 0