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>
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!
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.
精彩评论