Monday, March 26, 2012

Programming Validators

I have the following by it doesnt seem to catch the errors;

HTML:

<input id="textBox" /> <span id="textBoxVa" style="color: red">* </span
function pageLoad(){

var textBox = new Sys.UI.TextBox($('textBox'));
textBox.initialize();

var requiredValidator = new Sys.UI.RangeValidator();
requiredValidator.set_lowerBound (5);
requiredValidator.set_upperBound (15);
requiredValidator.set_dataContext(textBox);
requiredValidator.set_errorMessage("ERROR WITH INPUT");
requiredValidator.initialize();

varvalidationLabel = new Sys.UI.ValidationErrorLabel($('textBoxVa'));
validationLabel.set_associatedControl(textBox);
validationLabel.initialize();
}

If I enter 0 or 1000, it doesnt seem to show my little red dot :(I think you need to call setOwner on the validator to associate it with the textBox.
Thanks for the response.

I thought that was the problem originally but:

var textBox = new Sys.UI.TextBox($('textBox'));
textBox.set_text("HELLO");
textBox.initialize();

var requiredValidator = new Sys.UI.RangeValidator();
requiredValidator.set_lowerBound (5);
requiredValidator.set_upperBound (15);
requiredValidator.set_dataContext(textBox);
requiredValidator.set_errorMessage("ERROR WITH INPUT");
requiredValidator.setOwner(textBox);
requiredValidator.initialize();

varvalidationLabel = new Sys.UI.ValidationErrorLabel($('textBoxVa'));
validationLabel.set_associatedControl(textBox);
validationLabel.initialize();

alert(textBox.get_isInvalid ());

Still does not work :(

Right. My mistake. You need to add the validator to the textbox's validators collection. This should take care of calling setOwner so you don't need to do it.


Thanks Bleroy :)

Final code sniplet:

var textBox = new Sys.UI.TextBox($('textBox'));
textBox.set_text("HELLO");
textBox.initialize();

var requiredValidator = new Sys.UI.RangeValidator();
requiredValidator.set_lowerBound (5);
requiredValidator.set_upperBound (15);
requiredValidator.set_dataContext(textBox);
requiredValidator.set_errorMessage("ERROR WITH INPUT");
requiredValidator.initialize();

textBoxValidator = textBox.get_validators();
textBoxValidator.add(requiredValidator);
Alright a couple more questions that I ran into:

1) I dont really have a clue as to setting up a custom Validator
2) Same thing with setting up Group Validators

I'm not getting the display to show up for the validation label. The alert shows it's invalid, but nothing displays.

var

textBox;function pageLoad()

{

textBox =

new Sys.UI.TextBox($('textBox'));

textBox.initialize();

textBox.set_text(1);

var rangeValidator =new Sys.UI.RangeValidator();

rangeValidator.set_lowerBound(5);

rangeValidator.set_upperBound(15);

rangeValidator.set_dataContext(textBox);

rangeValidator.set_errorMessage(

"Must be between 5 and 15");

rangeValidator.initialize();

var textBoxValidator = textBox.get_validators();

textBoxValidator.add(rangeValidator);

var validationLabel =new Sys.UI.ValidationErrorLabel($('textBoxVa'));

validationLabel.set_associatedControl(textBox);

validationLabel.initialize();

alert(textBox.get_isInvalid());

}


It is a mouseover tooltip kinda thing ;)

No comments:

Post a Comment