Validation of .net form - asp.net

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.

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

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.

RequiredFieldValidator and CustomValidator fire the same time?

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

ASP.Net validation controls behaviour

Asp.net validation controls generates client-side javascript that validates controls.
I decided to have a look into the ASP.NET validation JavaScript and I can see there is a function that hooks into the onchange events for controls to make sure the validation script runs when a values is changed.
My question:
Is there a way to disable this onchange validation? I want the validation to be done on the submit and then show the necessary error messages.
Thanks
Simply set:
EnableClientScript="False"
On the control.

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