12

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

Community
  • 1
  • 1
Rich Andrews
  • 4,168
  • 3
  • 35
  • 48
  • I know this might be old but once the Validator has been activated onSubmit, how do you turn it off again on focus, so that the error message disappears until the submit button is clicked again? – RealSollyM Jul 03 '15 at 10:15

8 Answers8

5

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

Dejan
  • 501
  • 5
  • 13
  • I know this might be old but once the Validator has been activated onSubmit, how do you turn it off again on focus, so that the error message disappears until the submit button is clicked again? – RealSollyM Jul 03 '15 at 10:15
2

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

Ed B
  • 6,028
  • 3
  • 26
  • 35
0

You could turn off the javascript validation by setting EnableClientScript="false" that would get rid of the lost focus validation.

Chris Mullins
  • 6,677
  • 2
  • 31
  • 40
0

You can use Custom Validator controls instead and either validate the input using Javascript on the client or within the event handler on the server. Ensure you set ValidateEmptyText="true" on the validation controls otherwise the events will not fire on an empty field.

Sambo
  • 1,472
  • 5
  • 21
  • 33
0

Try to Enable on Both button click using javascript and disable it on textbox blur event.

Navin
  • 1
0

simply type this code in page_load event

textboxname.Attributes.Add("onblur","ValidatorOnChange(event);");
Scorpio
  • 2,309
  • 1
  • 27
  • 45
Aasim Inamdar
  • 331
  • 3
  • 4
0
var validator = document.getElementById('<%=Validator1.ClientID%>');
ValidatorEnable(validator , state); 

It is working in javascript but when we use the page.Isvalid function on Server side it creates the problem to check page is valid or not.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Ray
  • 1
0

Try resetting the onchange event for the input-control.

$(document).ready(function () {
        $("#controlid").each(function () { this.onchange = null; })
    });
Peter O.
  • 32,158
  • 14
  • 82
  • 96