Javascript Validation – Cross validation

  javascript, toolbox, validation

Scenario:
Field1: Primary field

Field2: Secondary field

The secondary field should be less than or equal to the primary field.

//fields
<input name="field1" id="field1" data-qid="1-f1" value="2"/>
<input name="field2" id="field2" data-qid="1-f2" value="5" crossvalidationqid="1-f1" toolboxcrossvalidation="Field1 should be higher than Field2"/>

//Validator
$.validator.addMethod('toolboxcrossvalidation', function customCrossValiadtion(value, element, param) {
if(this.optional(element)){
return true;
}

//Grab value of the input field field2
var inputValue = .....;
//Get compare element (field1) based on crossvalidationqid
var comapreelement = ......;
var compareElementValue = ...;

if(inputValue <= compareElementValue) {
return true;
}
$.validator.message["toolboxrossvalidation"] = element.attributes["toolboxcrossvalidation"].value;
return false;
});

Test case 1: Enter value 4 in field1, and enter value 5 in field2.
Validation is applied and error is displayed.

Test case 2:
field1 = 4 and field2 = 4
Change the value in field1 to 3.
Given field2’s value is still 4, but the validation error is not shown.

Could somebody please help me to rectify the code to satisfy test case 2 as well?

Thank you

Source: Ask Javascript Questions

LEAVE A COMMENT