onChange script for stop record submission if the validation doesn't meet

JaganN971616731
Tera Contributor

Hi Community,

 

I have a requirement that the field length should be minimum 25 characters with no blank space. If the validation doesn't meet an alert message should pop-up and shouldn't allow to save the record. I have written a onChange script, it is showing the pop-up message however it is allowing to save the record. Please help me in how to restrict to save the record if the validation doesn't meet?

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var char = newValue.toString();
    var str = char.replace(/ /g, "");
    if (str.length < 25) {
        alert('Problem statement should have minimum 25 characters excluding spaces.');
    }
}
 
Regards,
Jagan
7 REPLIES 7

surajchacherkar
Mega Guru

Hi @JaganN971616731 ,

try below code, you can use the gs.addErrorMessage() function to display an error message and return false to prevent the record from being saved

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var char = newValue.toString();
    var str = char.replace(/ /g, "");
    if (str.length < 25) {
        gs.addErrorMessage('Problem statement should have minimum 25 characters excluding spaces.');
        return false; // Prevent saving the record
    }
}

 

If my response helped you, please click on "Accept as solution" and mark it as helpful.


Thanks

Suraj!

HrishabhKumar
Kilo Sage

Hi @JaganN971616731 ,

You are using onChange Client script, that won't help to stop the form form submitting, instead you can use the same code logic in onSubmit client script. you just have to change a logic little bit, instead of using newValue you will have to get the value of the desired field using getValue.

 var char = g_form.getValue('my_field').toString(); // add the backend name of the field in place of "my_field".
    var str = char.replace(/ /g, "");
    if (str.length < 25) {
        alert('Problem statement should have minimum 25 characters excluding spaces.');
       //============================================================
        return false;
      //============================================================
    }

 

Thanks

 

Pratima Kalamka
Kilo Sage

Hello @JaganN971616731 

Please try below script using onsubmit client script and I have tried and it works .please try it and let me know.

function onSubmit() {
   //get the value of the field to validate 
   var fieldValue=g_form.getValue('description');
//check the lenth is less than 25 characters 
if(fieldValue.length<25 || fieldValue.indexOf('') === -1)
{
    g_form.addErrorMessage('Problem statement should have minimum 25 characters excluding spaces .');
    return false;
}
}
 
If my answer is helpful please mark it as helpful or correct!!
 
Pratima.k