Does not contain query in onsubmit script

chitra1
Tera Contributor

Hi All,

 

Basically, my requirement is a prefix check on the form , if the name does not contain the "GDC" prefix, the form should not be saved, preventing the user from changing the prefix.

 

Based on the organization, name prefix will automatically populate, attached screenshot for reference.

 

Tried this code: 

name1.addQuery('name','DOES NOT CONTAIN','IN-GDC');
 
Any suggestions please.
 
Thanks,
Chitra
6 REPLIES 6

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @chitra1 

 

You can use this onSubmit Client Script

function onSubmit() {
    var name = g_form.getValue('name'); //change variable name accordingly
    var prefix = 'IN-GDC';

    if (name.indexOf(prefix) !== 0) {
        g_form.addErrorMessage('Name must start with the prefix "GDC". Please correct before submitting the form.');
        return false; // Prevent form submission
    }

    return true; // Allow form submission
}

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Hi @Sohithanjan G ,

 

I have added the same script for other options as well,but its showing the IN-GDC error only.

 

chitra1_0-1710218731551.png

Thanks,

CD

Hi @chitra1 , 

You cannot use multiple logics here in that format. Try below

function onSubmit() {
    var name = g_form.getValue('name'); //change variable name accordingly
    var prefixes = ['IN-GDC', 'DC', /* Add more prefixes here */];

    var validPrefix = false;
    for (var i = 0; i < prefixes.length; i++) {
        if (name.indexOf(prefixes[i]) === 0) {
            validPrefix = true;
            break;
        }
    }

    if (!validPrefix) {
        var errorMessage = 'Name must start with one of the following prefixes: ';
        for (var j = 0; j < prefixes.length; j++) {
            errorMessage += '"' + prefixes[j] + '"';
            if (j < prefixes.length - 1) {
                errorMessage += ', ';
            }
        }
        errorMessage += '. Please correct before submitting the form.';
        g_form.addErrorMessage(errorMessage);
        return false; // Prevent form submission
    }

    return true; // Allow form submission
}

 




Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Hi Chitra,

You are getting this "IN-GDC" error only because the first line of if code is  not satisfying the condition when there is "IN_GDC" present in the name and it skips the if part and goes to else part and when "IN-GDC" is  present then it prints the error message from first if block only. In this case you can used nested if approach to get the desired result.