From: Richard on 8 Sep 2010 16:30 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
From: Scott M. on 11 Sep 2010 20:10 AFAIK each validator control MUST have its ControlToValidate property set. "Richard" <Richard(a)discussions.microsoft.com> wrote in message news:7BADA706-AFDC-4042-AD68-A29E2F36A2A2(a)microsoft.com... > 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 > > >
|
Pages: 1 Prev: Running problem Next: "Missing parameter values." in Crystal Reports |