RequiredFieldValidator and CustomValidator fire the same time? - asp.net

I have a asp.net file with:
3 radio boxes, one must be selected
a listbox in which a item must be selected
a captcha-validation
Now I have 4 Validators:
CustomValidator for the Radio boxes
CustomValidator for the Listbox
CustomValidator for the captcha-validation (which is saved in a session)
RequiredFieldValidator for the captcha-textbox not to be empty.
My problem is the order.
If a user try to sent the formular and all 4 validators are wrong, only the RequiredFieldValidator will be shown, because the other 3 are serverside.
The user now thinks "Oh, only one mistake" and corrected it.
And after that, 3 other mistakes occour and the user might be a little angry about that - "Why the system doen't tell me in first playce?!"
So, how is there a user-nice solutiton?
Is there a way to fire the server-side-validation at the same time, a client-side validation fails?
Or is there a way to fire the client-side validation on server side?
(Attention! I tried to remoce the RequiredFieldValidator and instead do a fourth CustomValidator. Wehn I do this, I have 4 CustomValidator and none Requ-Field-Val. It end up, that none CustomValidator will be hit, after the formular is send =( I don't know why.)

One of the way is to turn off client side validation for required field validator using EnableClientScript property. So that all your validations on the server side.
But better way (if possible) will be to use client side validations for all of them i.e. use custom js validation logic with your custom validators using ClientValidationFunction property - see this article for such example.

Add the ValidationGroup="abc" property for Controls validators and on Submit function Use Page.Validate("abc") method to have server-side controls validated at some Time.
Something like this.
Page.Validate("abc")
If Not Page.IsValid Then
Return False
End If

Related

Internal works of ASP.NET Validation controls in client and server side

I want to know how ASP.NET validation controls are working (eg: requiredfield validator, regular expression validator). What will really happening when clicking on the button? What are the events are fireing on client side? What really happening internally?
When you click on the button (if you you defined CausesValidation="true") button send request to server with __doPostBackWithOptions function aims which contains valiadtion group as parameter (if you defined ValidationGroup property for your button). __doPostBackWithOptions contains function Page_ClientValidate. In this case will be checked all validators which belong for this group. If result (Page_IsValid) will be true postback occures. If You don't define validationGroup (but CausesValidation true) will be checked all validators which is present on page. If defined CAusesValidation false will use _doPostBack function without validators checking.
More information here: http://msdn.microsoft.com/en-us/library/aa338815(v=vs.71).aspx and
http://msdn.microsoft.com/en-us/library/aa479045.aspx

Validation of .net form

Is it possible to use javascript validation and asp.net validators (required field validator) simultaneously on a single button click. Please help me
ASP.NET validators support clientside validation.
Have a look: ASP.NET Validation in Depth, chapter "Client-Side Validation"
You need to set EnableClientScript to true (but it's the default value anyway).
So the RequiredFieldValidator checks on the client side if the user entered something.

I'm having trouble understanding page validation

Okay, so I have my asp.net page with all of my comparison and requiredfield validators. This leaves me with two concerns.
What additional validation do I need? Do I need anything in the code behind? I want them to be unable to hit the 'save' button until their textbox information is complete, and it seems to be doing this with just the validator controls, but I'm unsure if there are other steps I need to take.
If I have a requiredfield validator and I want to turn it off under special circumstances, where in the codebehind would I set it to true? Can I do it on the 'save' button click, before it prevents the button from functioning?
1.
You need as many validations as necessary, you can create many different validators.
You need server-side validation in the code behind. If a postback occurred then the form passed the validation, but there are some validation features which are unusable at client-side. For instance you register to a homepage and you have a form where the username is required and there is a regular expression validator too. These validators will run at client-side. But if the username has to be unique and you can only check that using a database then obviously this can't be checked at the client-side, therefore, the client-side will evaluate the page to be valid, a postback will occur and it's the job of the server-side to check whether the username is unique.
Note that you can create custom validators if you need to do anything exotic.
2.
Depending on your needs you can set the Enabled property of your validators whenever you need to do that. Read more about that here.
As per my knowledge,
We should include a check for "Page.IsValid" on the server side (code behind) whenever we are using the ASP.NET Validators. This would ensure a check at the server side even if the javascript is being disabled on the browser.
No, you can't do that on the save button click as the button click would not be hit until the validation passes.
Hope this Helps!!
1 if you want add validation server
protected void Button1_Click(object sender, EventArgs e) {
//Proceed only if the validation is successfull
if (!Page.IsValid) {
return;}
}
2 You can set CausesValidation="false" to button
The #1 question with Page.IsValid is ok.
The requiredfieldvalidator is a javascript validator, so you can't disable it from codebehind once it was enabled (unless it satisfies the condition and you can go through). But it's possible to disable it via javascript, check this code:
ValidatorEnable(workPhoneValidator, false);
Link: Dynamically enable or disable RequiredFieldValidator based on value of DropDownList

Server side validation using validation controls

I have a form with the following controls
textbox for emailId -txtEmail
textbox for aboutme- txtAbout
submit button -btnSubmit
I'm using requiredfieldvalidators and reqularexpression validators for both the text fields.
I need to do serverside validations. So I've set EnableClientScript to false.
txtAbout and btnSubmit should be disabled if txtEmail is either empty or invalid. How do I fire the validation for this?
Also, if I'm asked to do Server Side validation, should I EnableClientScript to false, 'coz I read somewhere that even if is set to true, server side validation will fire.
txtAbout and btnSubmit should be disabled if txtEmail is either empty
or invalid. How do I fire the validation for this?
In order to implement this, according to my understanding you need to do client side validation. At client side you can use key press/Up events and you can use regular expressions i.e. RegExp in the javascript to validate the Email address textbox...and the corresponding underneath controls can be Enabled/Disabled on the basis of Email validation.

ASP.NET - how to stop unrequired server validation

I have used ValidatorEnable to disable a RequiredFieldValidator in javascript. However on postback the control being validated by the validator is still validated ... even though I disabled the validator on the client.
I understand why this is happening since I've only disabled the client validation ... however is there a nice way to determine that I've disabled the client validation and to then disable the server validation on postback?
You could, when you disable the client validation (presumably with JavasScript?), also set up values in a hidden input that you could query in page load method. This way you can examine the value of the hidden input (via the Request.Form{] array) to disable the validators on the server side prior to the validation event firing.
Another option would be to override the pages Validate() method to disable the validators based on the same rules that hid them on the client side.
It's not clear from your question, what you are disabling. The RequiredFieldValidator has an both an Enabled and an EnableClientScript property.
If you want to disable validation on both client and server you should set Enabled to false.
To disable just client side, set EnableClientScript to false.
Peanut, I just encounted the same issue you are. On the client-side I have javascript that disables required field validators depending on their selections in the UI. However, the server side validation was still firing.
During load, I added a method to disable my validators using the same rules I have on the javascript. In my case, it depended on the user's selection of a radio button:
this.requiredValidator.Enabled = this.myRadioButton.Checked;
This seems to work on the SECOND page load, but not the first. After debugging, I discovered that the wrong radio button is considered to be checked when this line was firing. The View State/Post Data wasn't applied to the control at the point when I was checking it to disable my validator. The reason I placed this during load was to make sure I disabled the validator before it fired.
So, the solution seems to be to have something like the line above AFTER ViewState, but BEFORE validators.
Overloading the Validate() method, as palehorse suggested, worked and allowed me to ensure the server side validators were enabled/disabled based on the current selections of the user.

Resources