Validating two inputs: either both empty or both not empty

Dear DHTMLX-Community,

I created a form with multiple inputs. Two of those inputs are related (together, they represent a point (x-/y-coordinates). Specifying a point is optional, so the form is valid if both inputs are empty. If just one input is empty, the form is invalid, since a point in a plane is defined by two coordinates.

For this reason, I have two requirements for validating those two inputs:

  1. requirement: Validation should fail, if exactly one of the inputs is empty.
  2. requirement: If validation fails, both inputs should receive the red highlight that indicates validation failure. As an alternative, only the empty input should be highlighted.

This is an extract of my current code:

function validation(inputValue, inputName) {
	var otherInputName = (inputName === "firstInput") ? "secondInput" : "firstInput";
	var otherInputValue = form.getItemValue(otherInputName);
	if (inputValue !== "" && otherInputValue === "") {
		return false;
	}
	else return true;
}
form.setValidation("firstInput", validation);
form.setValidation("secondInput", validation);

I believe that my code fulfills the first requirement. However, I have so far been unable to highlight both inputs in case of validation failure.
Any help would be greatly appreciated.

Fabian

I think your function should be called validImageCoordinate.

Also you will need to call
form.enableLiveValidation(true);
or
form.validate()
to trigger the validate functionally.

I’m no expert of the component, i’ve been working with them for a few days, so take it with a grain of salt.

Unfortunately it is not available in this case to highlight both inputs at a time.

You are right, the function names have to match. I renamed the function for this extract and forgot to change the references to it as well. I will edit the code accordingly. I am using live-validation, but I chose not to include this part in my extract.
Anyway, thank you for your reply!