How to prevent changing values in a field from onChange client script?

Abhrajyoti Kan1
Tera Contributor

I have a requirement to prevent changing values of certain field from onChange client script.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading) {
		return;
   }

	if ( newValue === '' ) {
		var ans = confirm("Confirmation message.");
		if (ans == true){
			g_form.setValue('comments', " ");
		}
		else if (ans == false){
          // Need to prevent changing values here
			return;
		}
		
	}
   
}

What are the possible ways that I can prevent changing values of the field based on condition?

6 REPLIES 6

Abhinay Erra
Giga Sage

You can always set it back to old value using oldValue variable defined in the onchange client script.

oldValue will only hold the value which will be there in the field while loading the form. In this case, I cannot set the value to oldValue beacuse if I change value from valueA to valueB and again valueB to valueC without saving the form, oldValue will always hold valueA. 

I need something that, if the value changed from valueA to valueB and again valueB to valueC, and if valueC is selected from choice the field value not supposed to be changed. i.e. the valueB will remain in that field.

 

Hope I could make you understand my concern.

Sathish
Tera Contributor

Did you find any solution for this?

suvro
Mega Sage
Mega Sage

Hi Abhrajyoti,

 

If you do not want the user to select a particular option you can use removeOption() method to achieve this. If the option is not available then user will not be able to select it.

So when user selects the value as B then in the Onchange client script you can check if the newValue == B then use removeOption() method to remove the option C to be selected