I’m not sure how to achieve the following in javascript, or even if I’m thinking about it correctly. Basically I want to attach a javascript member function to each custom object rendered, so I could have something like this in C#:
public class NumericTextBox : TextBox
{
...
string clientScript = "function isValid() { return isNumericValue(this.value); }";
AttachValidationFunction(clientScript);
}
public class EmailTextBox : TextBox
{
...
string clientScript = "function isValid() { return isEmail(this.value); }";
AttachValidationFunction(clientScript);
}
and then use the following javascript function in the page
function isFormValid() {
var controls = getElementsByClass("validatingControl");
...
if (!controls[i].isValid()) return false;
...
}
obvisouly in pseudo-code, but hopefully that gives the idea of what I need to achieve. Any suggestions?
what i would do, is to make sure that AttachValidationFunction receives
this.ClientIDalong with the validation function, adds to a list (say,Dictionary<String, String>) and at render time, registers a javascript block with an array of all the added controls, where the result would look like this:And then you could iterate over that array like so:
I hope that’s quite clear.