Can you tell me when will use CustomValidator. For eg I have Textbox that accept comments from the user. Any reason why the CustomValidator would fit in scenario and what other validators cannot do or is difficult to do?
Your question describes the exact reason to use a CustomValidator: you use it when the existing validators won't do what you need done. For instance, there is no validator that will ensure that the value in text box B is between the values in A and C. You'd have to do that in code, in a CustomValidator.
CustomValidator is meant to give you freedom to implement your own validation logic where other validators just don't do what you need to do.
E.G: When you need to do any kind of validation that doesn't actually require a user input control (such as checking if a cookie exists) or if the control you want to validate has to follow input rules that are too involved for the other validators.
It depends what you are trying to validate - you will need to provide more information.
The CustomValidator lets you write any code you want to perform one or more validations on the data entered into the Textbox. The other validators perform a distinct function like comparing values, checking that a value has been entered or checking that a value conforms to a regular expression.
One thing I've used CustomValidators for in the past was generate a list of error messages to go in a ValidationSummary control. In general, use them whenever you would want to do some custom validation that the standard set of ASP.NET validators don't cover.
Related
I'm writing an ASP.NET page and trying to get validation working. My problem is that I've got a repeater that contains several custom grid controls, each of which has validators and a validation summary.
At first, I didn't assign any validation groups, but this ended up making validation summaries appear for every grid whenever there was an error in one (because the validation summary's validation group was not set, I suppose, which meant it captured all validation errors).
So I assigned a separate validation group for each grid, unique with respect to a certain property on the control. But I have a button at the bottom of the page (with no validation group) that needs to validate the input. Not having a validation group, it won't automatically validate the grids' validators, so I added a click handler that calls Page_ClientValidate(). No dice--validation appears everywhere.
Okay, so I iterate through the validation groups and call Page_ClientValidate(validationGroup) on each if it has any validators. Works fine when only one grid has validators, but when two or more do, it automatically hides all validation summaries but the last one checked. Is there any way to disable this behavior, or a better way to do this entirely?
If I need to, I guess I can go through and unhide the other validation summaries once I've finished iteratively validating (although that might possibly have other implications), but I'd also need to unhide the validator displays (I'm using an image to denote invalid fields). It seems like such an annoying and possibly fragile solution.
EDIT: Oh, a twist. I tried doing the approach I mentioned at the end--re-showing the hidden validators/validation summaries that are invalid--but the Microsoft code prevents that, too; in the first line of the ValidatorValidate(validator, validationGroup, event) method (called by Page_ClientValidate(validationGroup) on each validator in the page), validator.isvalid is set to true, and is only set to the return value of the validation function within a conditional that only runs if the validationGroup parameter matches the validator's. The upshot being, the hidden validators all are marked as valid, making it tough to determine after the fact whether a validator is valid because it's actually valid or because Microsoft was stupid in designing the client-side validation code.
Hi
I need to validate two fields in an Asp.net form, where the requirements is like any one of them is required. For example, there is Page title and sub-heading input boxes, so any one of them is required. Can I do it using the validation controls Asp.Net provides?
Any kind of help is greatly appreciated. Thanks in advance.
You can use a CustomValidator control (MSDN) in ASP.NET for special situations that are not supported by any one of the other standard validators. It was created for this reason.
Microsoft describes how to create a custom validation function here.
Here's another tutorial on implementing it.
Or if you google for keywords like "creating a custom validator in asp.net" you can pick and choose from various solutions for your own project.
For your case, as an alternative to using a CustomValidator, you could explicitly change whether your required field validators are enabled by using the ValidatorEnable() JavaScript function.
// disable validation control
ValidatorEnable(RequiredFieldValidator1, false);
You can then write custom logic in JavaScript to determine the case in which each validation control is either enabled or disabled, and tie it to one of the (client-side) events of the text boxes (onblur, onchange, onkeyup, etc).
Then, on the server side you can write similar logic to do the same thing by setting the "Enabled" property and put this logic in your button click event before you check the IsValid state.
If all you are doing is conditionally determining when something is required, changing the enabled state is your best bet. Exactly what can be done is documented in ASP.NET Validation In Depth.
I have 4 textboxes which can only take a number. The problem is that I have to create 4 separate validators for the controls and associate their id with the validator. Is it possible to have just one asp.net regular expression validator and associate all the controls with that one validator?
You could use CustomValidator and write your own validation code. The clientside code could hypothetically access each text box and check them against the regex...
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
A simpler solution if you don't want to have to include the regex in 4 places is to store the regular expression in the code-behind and apply it to all the validators during Page Load? Not a perfect sol'n but would work. You could also apply a standard error message and other formatting options in the code-behind.
No, because each validator has a "controltovalidate" property that can only be set to a single value. The only exception is the CustomValidator
I want to do validation in asp.net with javascript on age and mobile number. Can you suggest how I could do this?
Check out jQuery and the jQuery validation plugin which will handle these either directly or via the ability to extend the validation rules. That's the code that I would use.
For the age validation you can use a RangeValidator and for the mobile number you can use a RegularExpression validator.
For age you will want a validator that restricts the textbox to numbers only
For mobile number, you will want numbers only, and also "+" and maybe "-"
I don't want to write a tutorial on validator controls here, but you'll need a RangeValidator* for the age and a RegularExpressionValidator with two regexes and a ValidationSummary control for the error messages. Set EnableClientScript on the two validators to true.
There's a large amount of How Tos on the MSDN site.
* I suggested the wrong one there initially and copied George for RangeValidator
I'm looking to make validation for a page in which one or more fields have a value in them. I have an advanced search form in asp.net and I'm trying to pop up an error and not post back if all the fields are empty. I've looked into required fields validators but I'm not sure how to make them work together in a AND type fashion instead of the OR fashion that a validation group of required field validators would imply. I hope this makes sense. Thanks for the help.
i had to do something similar years ago and i was using 1.1 then.
what we ended up doing was creating required field validators but disabling them.
then onload we would loop through the validator dictionary, enable them and check if they passed. if any of them passed we broke off the loop and we continued execution, otherwise, if all of them failed then we displayed a warning. Unfortunately, this would require a postback.
If you want to accomplish this on the client-side then you could write a simple javascript function to take care of it before postback.
for every control, place an onBlur event. javascript will check if there is a value in the field and maintain a flag. then before submission you would check the flag and either allow submission or show warning.
You could just write the javascript validation function yourself to handle this case and attach it to your search button.