How to change control's background color when validation fails when controls have multiple validation
Here is the java script :
function ContainsControl(controlArray,arrayItem) { var result = { boolValue: false, indexValue: -1 }; for (var i = 0; i < controlArray.length; i++) { if (controlArray[i]["control"] == arrayItem.control) { result.boolValue = true; result.indexValue = i; break; } } return result; } function SetBGColor() { var inputControlValidationResult = new Array(); for (var i = 0; i < Page_Validators.length; i++) { var inputControl = document.getElementById(Page_Validators[i].controltovalidate); var arrayItem = { control: inputControl, valid: Page_Validators[i].isvalid ? 1 : 0 }; var result = ContainsControl(inputControlValidationResult, arrayItem); if (result.boolValue) { inputControlValidationResult[result.indexValue].valid &= arrayItem.valid; } else { inputControlValidationResult.push(arrayItem); } } for (var i2=0; i2 < inputControlValidationResult.length; i2++) { inputControlValidationResult[i2].control.style.backgroundColor = inputControlValidationResult[i2].valid ? "" : "red"; } }
In order to run this script after client side validation but before page
submission, you have to register this using some of the server side
page events, I use the Page_Load :
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(GetType(), "onsubmit", "SetBGColor();");
}
Comments
Post a Comment