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

HrishabhKumar
Kilo Sage

Hi @JaganN971616731 ,

You have to return false in order to stop form submission. I have added it in your code, and below is the modified version of your code.

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.');
       //============================================================
        return false;
      //============================================================
    }
}

 

Thanks,

Hope this helps.

Hi @HrishabhKumar 

 

I tried with "return false" still it is not working.

 

Regards,

Jagan

Hi @JaganN971616731 ,

Remove the alert form the code, and if it works after that, then you can use AddErrorMessage instead.

Ashish37
Mega Guru

Hi @JaganN971616731  ,

 

OnChange client script is good you can keep it, but I think you need one more Client Script OnSubmit to check the field value before submit and throw error message because onChange client script wont prevent user from submitting the record.

 

something like this:

var fieldValue= g_form.getValue("field_name");

var str = fieldValue.replace(/ /g, "");
    if (str.length < 25) {
         g_form.addErrorMessage('Problem statement should have minimum 25 characters excluding spaces.');
    }
 
Hope this helps.
 
Thanks,
Ashish