javascript:
var OriginalValidatorUpdateDisplay = null;
function NewValidatorUpdateDisplay(val) {
OriginalValidatorUpdateDisplay(val);
if (val.controltovalidate) {
if (!CheckValidatorsForControl(document.getElementById(val.controltovalidate))) {
if (document.getElementById(val.controltovalidate).toString() == “[object HTMLSelectElement]“)
document.getElementById(val.controltovalidate).style.backgroundColor = “#FFAAAA”;
else
document.getElementById(val.controltovalidate).style.background = “#FFAAAA”;
}
else {
if (document.getElementById(val.controltovalidate).toString() == “[object HTMLSelectElement]“)
document.getElementById(val.controltovalidate).style.backgroundColor = “”;
else
document.getElementById(val.controltovalidate).style.background = “#FCFCFC”;
}
}
}
if (typeof (ValidatorUpdateDisplay) == ‘function’) {
OriginalValidatorUpdateDisplay = ValidatorUpdateDisplay;
ValidatorUpdateDisplay = NewValidatorUpdateDisplay;
}
function CheckValidatorsForControl(control) {
for (var i = 0; i < control.Validators.length; i++) {
if (!control.Validators[i].isvalid) {
return false;
}
}
return true;
}
function fnOnUpdateValidators() {
for (var i = 0; i < Page_Validators.length; i++) {
var val = Page_Validators[i];
var ctrl = document.getElementById(val.controltovalidate);
if (ctrl != null && ctrl.style != null) {
if (!val.isvalid) {
if (ctrl.toString() == “[object HTMLSelectElement]“)
ctrl.style.backgroundColor = “#FFAAAA”;
else
ctrl.style.background = “#FFAAAA”;
}
else {
if (ctrl.toString() == “[object HTMLSelectElement]“)
ctrl.style.backgroundColor = “”;
else
ctrl.style.background = “#FCFCFC”;
}
}
}
}
C# code :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), “val”, “fnOnUpdateValidators();”);
}
it seems like it does not work if there are multiple validators on 1 control
i have a problem with 1 requiredfieldval and 1 regular expression validator on a textbox “email address”
only 1 validator colors red, the 2nd does not give a color
can you confirm? i have tried everything i could to resolve but did not find a solutoni,
you can check online on http://www.securitas-home-security.be/contact/
you can do it by changing one method =>
function fnOnUpdateValidators() {
for (var i = 0; i < Page_Validators.length; i++) {
var val = Page_Validators[i];
var ctrl = document.getElementById(val.controltovalidate);
if (ctrl != null && ctrl.style != null) {
if (!CheckValidatorsForControl(ctrl)) {
if (ctrl.toString() == “[object HTMLSelectElement]“)
ctrl.style.backgroundColor = “#FFAAAA”;
else
ctrl.style.background = “#FFAAAA”;
}
else {
if (ctrl.toString() == “[object HTMLSelectElement]“)
ctrl.style.backgroundColor = “”;
else
ctrl.style.background = “#FCFCFC”;
}
}
}
}
This works fine, thanks for this,
i have discovered if there is a radiobuttonlist in the form the script delivers an error “Validators.Length” is Null or not an Object
> i have a autopostback on the form, you can check on
http://www.securitas-home-security.be/bestel-2/ do a autopostback do change some dropdownlist and you will see the error (because there is a radiobuttonlist with a validator on the page)
the script works with autopostback (without radiobuttonlist) check
on http://www.securitas-home-security.be/bestel/
maybe you know how to solve this? would be a great help (i can do a donation for this, if neccessary :-))
You have to change one method again.
function CheckValidatorsForControl(control) {
if (control.toString() == “[object HTMLTableElement]”) {
for (var i = 0; ; i++) {
var ele = document.getElementById(control.id + “_” + i);
if (ele == null)
break;
if (ele.checked == true)
return true;
}
return false;
}
else {
for (var i = 0; i < control.Validators.length; i++) {
if (!control.Validators[i].isvalid) {
return false;
}
}
}
return true;
}
Thanks, you do not have to donate 😀
hey Thanks, but there is still an error,
Bericht: ‘Validators.length’ is emtpy or no object
Regel: 38
Teken: 25
Code: 0
URI: http://www.securitas-home-security.be/js/validator.js
this is my full JS
var OriginalValidatorUpdateDisplay = null;
function NewValidatorUpdateDisplay(val) {
OriginalValidatorUpdateDisplay(val);
if (val.controltovalidate) {
if (!CheckValidatorsForControl(document.getElementById(val.controltovalidate))) {
if (document.getElementById(val.controltovalidate).toString() == “[object HTMLSelectElement]”)
document.getElementById(val.controltovalidate).style.backgroundColor = “#FFAAAA”;
else
document.getElementById(val.controltovalidate).style.background = “#FFAAAA”;
}
else {
if (document.getElementById(val.controltovalidate).toString() == “[object HTMLSelectElement]”)
document.getElementById(val.controltovalidate).style.backgroundColor = “”;
else
document.getElementById(val.controltovalidate).style.background = “#FFFFFF”;
}
}
}
if (typeof (ValidatorUpdateDisplay) == ‘function’) {
OriginalValidatorUpdateDisplay = ValidatorUpdateDisplay;
ValidatorUpdateDisplay = NewValidatorUpdateDisplay;
}
function CheckValidatorsForControl(control) {
if (control.toString() == “[object HTMLTableElement]”) {
for (var i = 0; ; i++) {
var ele = document.getElementById(control.id + “_” + i);
if (ele == null)
break;
if (ele.checked == true)
return true;
}
return false;
}
else {
for (var i = 0; i < control.Validators.length; i++) {
if (!control.Validators[i].isvalid) {
return false;
}
}
}
return true;
}
function fnOnUpdateValidators() {
for (var i = 0; i < Page_Validators.length; i++) {
var val = Page_Validators[i];
var ctrl = document.getElementById(val.controltovalidate);
if (ctrl != null && ctrl.style != null) {
if (!val.isvalid) {
if (ctrl.toString() == "[object HTMLSelectElement]")
ctrl.style.backgroundColor = "#FFAAAA";
else
ctrl.style.background = "#FFAAAA";
}
else {
if (ctrl.toString() == "[object HTMLSelectElement]")
ctrl.style.backgroundColor = "";
else {
if (ctrl != null) {
ctrl.style.background = CheckValidatorsForControl(ctrl) ? "#FFFFFF" : "#FFAAAA";
}
}
}
}
}
}
Hey Shakil,
i have still this problem (even after your great help :-))
you can see it here
http://www.viavacation.com/nl/account/acco-add-7/
when you click on the submit button without choosing a radiobuttonlist item, > it returns a JavaScript error, i really can not find where the problme is,
cna i hire you to help me to debug this JS?
> JS file
http://www.viavacation.com/js/validator.js
thanks a lot,
regards
Wouter
Today is my eid day. I will available for work after 6 days. After 6 days, i will view your code. Thanks 😀
Wouter,
Please, read the below comments of swamy. Thanks
Hi wouter,
i found the the rootcause of js error “when you click on the submit button without choosing a radiobuttonlist item, > it returns a JavaScript error
And below is page, when page is loaded the two radiobutton, one is selected by default thats why the error not come up .
“http://www.securitas-home-security.be/bestel/”
RootCause
Issue is thew two radio button is not selected by default,but in this aspx page have requiredFieldValidator so this be controltoValidate property to the radiobutton
Solution
1) please modify In your aspx page have two radio button, any one should have checked by default then doesnt throw the error — this is common and very easy method to solve it
EX: add the Checked=”True”
or
2) when you click on the submit button we have to validate if not select the radio button,should not throw the error using requireFiled validator control,otherwiseif select any one of the radio button it shows the appropriate result
Thanks,
swamy
On Wed, Aug 31, 2011 at 2:06 AM, Wouter
This is not à splition
The visitor has to choose no default value set
So In this Case, you have to choos the 2nd option
below is the solution
when you click on the submit button we have to validate if not select the radio button,should not throw the error using requireFiled validator control,otherwiseif select any one of the radio button it shows the appropriate result
Thanks,
swamy
So you have to remove the ControltoValidate=”RadiobuttonID” from the RequireFieldValidator .
Then it works
Thanks,
swamy
Hey Shakil,
i have easly fixed it by at and extra line,
if (control.type == “radio”) {
>>
function CheckValidatorsForControl(control) {
if (control.type == “radio”) {
for (var i = 0; i < control.Validators.length; i++) {
if (!control.Validators[i].isvalid) {
return false;
}
}
return true;
}
}
live can be easy !
thanks a lot for the updates