views:

115

answers:

2

Is it possible to capture what button was clicked in javascript? I have validation set up but i only want it to validate when a button is clicked and by no other means.

PLEASE NOTE THAT I HAVE 3 TEXTBOXES JUST LIKE THE BELOW. IT'S FOR A PHONE NUMBER ENTRY BROKEN UP INTO 3 TEXT BOXES.

What i would like to do is put and '&&' condition in that javascript if statement saying to set the args.IsValid to false when the button is clicked. Right now it validates on tab key and click of other controls. i just want it to validate on the click of the submit button.

function checkPhNumn(sender, args) {
        alert(window.event);
        if (phnavalue.value != '' || phnevalue.value != '' || phnnvalue.value != '' ) {
            if (phnnvalue.value.length < 4) {
                args.IsValid = false;
            }
            else {
                ValidatorEnable(RFV2, true);
                ValidatorEnable(RFV3, true);
            }
        }
    }

 <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender4" runat="server" 
                            TargetControlID="phnnVal" 
                            HighlightCssClass="validatorCalloutHighlight"
                            ></ajaxToolkit:ValidatorCalloutExtender>

<asp:TextBox ID="witPhnn" runat="server" MaxLength="4" Width="50pt"></asp:TextBox>

                        <asp:CustomValidator ID="phnNumValn" runat="server" 
                            Display="None" 
                            ControlToValidate="witPhnn"
                            ErrorMessage="Please enter a valid phone number." 
                            SetFocusOnError="True"
                            EnableClientScript="true" 
                            ClientValidationFunction="checkPhNumn"
                            ></asp:CustomValidator>
+1  A: 

Are you using ASP.NET 2.0?

If so, there is a property for buttons and validators called "Validation Group". By specifying a name for this property, validation will only fire for those with matching validation group values.

For example, let's say you have two required field validators on your page with their Validation Group values set as "group1" and "group2" respectively. You later on add a button control and give that control's Validation Group property a value of "group1".

When that button is pressed, only the validators for "group1" will actually fire.

Hope this helps!

jlech
Yes, but i don't thing it will help because the button fires off validation for the whole page. The entire page would be in the same group. I'll try though. Thanks for your reply.
Eric
Thanks But it didn't work.
Eric
A: 

Remove the ControlToValidate from your CustomValidator, which will remove the call to the validation function when you leave the textbox. Using ValidationGroups or setting CausesValidation = False on all the other buttons will prevent them from activating any validation.

Jason Berkan