Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i turn off ASP.NET required field validator "lost focus" behaviour

I have some code where I need two separate required field validators for one control, both in separate validation groups which are then validated by two separate buttons.

This approach works well when the buttons are clicked but both validators show if I enter a value in the textbox and then remove it.

Is there a way to turn this"lost focus" validation off? I only need it to validate when the buttons are clicked.

EDIT

Unfortunately, if I set EnableClientScript=false then I dont have any client notifications. What I want is for the dynamic error message to show (effectivly in the OnClientClick event of the button) but not the "lost focus" of the textbox.

Is there some way I can disable or "unhook" the lostfocus client event?

EDIT

A combination dDejan's answer and womp's answeer here sorted the problem perfectly.

My final code looks like this (for anyone else with a similar situation)...

Javascript...

<script type="text/javascript">

    $(document).ready(function() {
        $('body').fadeIn(500);

        //Turn off all validation = its switched on dynamically
        $.each(Page_Validators, function(index, validator) {
                ValidatorEnable(validator, false);
        });
    });

    function ToggleValidators(GroupName) {

        $.each(Page_Validators, function(index, validator) {
            if (validator.validationGroup == GroupName) {

                ValidatorEnable(validator, true);

            } else {
                ValidatorEnable(validator, false);
            }
        });
    }

</script>

ASPX Control Example...

<telerik:RadTextBox Width="196px" ID="txtFirstName" runat="server" MaxLength="50" Skin="Black"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="valFirstName" CssClass="Validator" runat="server" EnableClientScript="true" Display="Dynamic" ErrorMessage="You  must enter your first name." ControlToValidate="txtFirstName" ValidationGroup="NeededForEmail"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" CssClass="Validator" runat="server" EnableClientScript="true" Display="Dynamic" ErrorMessage="You  must enter your first name." ControlToValidate="txtFirstName" ValidationGroup="NeededForSubmit"></asp:RequiredFieldValidator>

ASPX Button Code...

<asp:Button ID="btnGetConfCode" runat="server" Text="Get Confirmation Code" OnClientClick="ToggleValidators('NeededForEmail')" OnClick="btnGetConfCode_Click" Width="100%" ValidationGroup="NeededForEmail"/>

<asp:Button ID="btnRegisterUser" runat="server" Text="Register" OnClientClick="ToggleValidators('NeededForSubmit')" OnClick="btnRegisterUser_Click" Width="100px" ValidationGroup="NeededForSubmit" />

So, now there is no validation until a user clicks either the "Get Email Confirmation Code" button or the "Register" button.

If they click the "Get Email Confirmation Code" button all of the controls validate apart from the textbox where the user is to input the email validation code and we only see one validator message.

If they click the "Register" Button then all of the controls validate and we only see one validation message.

If either button is pressed, the user goes back, adds and then removes some text then we only see one validator. Before this change you used to see both messages saying the same thing.

Thank you for help guys

like image 571
Rich Andrews Avatar asked Jun 23 '10 14:06

Rich Andrews


People also ask

How do I turn off required field validator?

Well you can simple use the Enabled="false" property of RequiredFieldValidator .

What is the use of required field validator in asp net?

The RequiredFieldValidator control enables you to require a user to enter a value into a form field before submitting the form. You must set two important properties when using the RequiredFieldValdiator control: ControlToValidate— The ID of the form field being validated.

Why do we need required field validator?

ASP.NET RequiredFieldValidator Control This validator is used to make an input control required. It will throw an error if user leaves input control empty. It is used to mandate form control required and restrict the user to provide data.


2 Answers

You can set if the validators are "active" or not with client side code using the ValidatorEnable function. Basically it goes like this

var validator = document.getElementById('<%=Validator1.ClientID%>');
ValidatorEnable(validator , state); //where state is boolean

You can also trigger the validator to validate on some event (like for example the click of the buttons) using the ValidatorValidate(validator) function.

I am not sure which would work better for you (enabling/disabling the validators or custom triggering of the validation) but I suggest this article that will guide you in the right direction

ASP.NET Validation in Depth

like image 93
Dejan Avatar answered Sep 28 '22 11:09

Dejan


There's no way to unhook them if EnableClientScript=true.

What you could do is set it to false. Then create a javascript validation method that is called on your submit-button onClientClick event.

In your method, you would have to call ValidatorValidate(control) for each control you want to validate client side

There's an example here:

http://msdn.microsoft.com/en-us/library/Aa479045#aspplusvalid_clientside

like image 41
Ed B Avatar answered Sep 28 '22 13:09

Ed B