ASP - NET - 2 - CustomValidator - Javascript - Custom msg not show

  • Thread starter Thread starter Richard
  • Start date Start date
R

Richard

Hi,

On an ASP.NET 2 there's a CustomValidator with EnableClientScript="true" and
the ClientValidationFunction set to a Javascript function shown below. The
validator has no ControlToValidate b/c inside the function several controls,
and not just one need to be validated.

The need is to have a validation control that fires, and sets the IsValid
appropriately on the page, and allows the displayed message to be set by code
in the client (Javascript). What we don't want is to have multiple validation
controls on the page just to perform simple validation.

The Javascript function that handles the ClientValidationFunction property
of the customvalidator is:

function CustValMyForm(source, arguments)
{
arguments.IsValid = ValMyForm(); // This function performs the
actual validation.
return;
}

function ValMyForm()
{
var txtFileCode =
document.getElementById("<%=txtFileCode.ClientId %>");
var cvError = document.getElementById("<%=cvErrorRen.ClientId
%>");

var sFileCode = Trim(txtFileCode.value);

if (sFileCode.length < 1)
{
cvError.InnerText = "Please enter a value.";
txtFileCode.focus();
return false;
}

if (IsNotNumeric(sFileCode))
{
cvError.InnerText = "Value must be numeric.";
txtFileCode.focus();
return false;
}

// Code continues...other controls are validated, etc...

return true;
}

The CustomValidator does fire, and it calls the functions in Javascript, but
whatever value is set in the Text property of the CustomValidator on the page
at design time, is displayed when an invalid value is detected by the
validation. The Javascript code somehow fails to change the message according
to the error.
The following variations have been tested, and the result is the same:
a) Using InnerText: cvError.InnerText = "Value must be
numeric.";
b) Using InnerHTML: cvError.InnerHTML = "Value must be
numeric.";
c) Using sender.errormessage: sender.errormessage = "Value must be
numeric.";

This is how the custom validator gets rendered on the resulting webpage:

<span id="ctl00_cphMyMaster_cvError"
style="display:inline-block;color:Red;width:160px;visibility:hidden;"></span>

And there's no way to change the "visibility:hidden" part of the style
attribute either from the Javascript function or by setting it on the
CustomValidator at design time.
This is a simple validation... why does it have to be such a headache!?

Your help will be greatly appreciated.

Thanks in advance,

Richard
 
Back
Top