determine which specific button triggered the validation process

Is there a way to identify which button was clicked using JavaScript? I have validation in place, but I want it to only validate when a specific button is clicked and not when other controls are interacted with.

PLEASE NOTE THAT I HAVE 3 TEXTBOXES SIMILAR TO THE ONE BELOW. THEY ARE USED FOR ENTERING A PHONE NUMBER SPLIT INTO 3 PARTS.

What I am trying to achieve is to add an '&&' condition in the JavaScript if statement to set args.IsValid to false only when the submit button is clicked. Currently, validation occurs on pressing the tab key or clicking on other controls, but I want it to happen only when the submit button is clicked.

function checkPhoneNumber(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="checkPhoneNumber"
                            ></asp:CustomValidator>

Answer №1

Do you utilize ASP.NET 2.0?

If you're using ASP.NET 2.0, there's a handy property for buttons and validators known as "Validation Group". By assigning a specific name to this property, validation will only trigger for elements with matching validation group values.

For instance, imagine you have two required field validators on your webpage with Validation Group values of "group1" and "group2" respectively. Later on, you introduce a button control and set its Validation Group property to "group1".

When that button is clicked, only the validators associated with "group1" will execute.

I hope this explanation proves useful!

Answer №2

To eliminate the ControlToValidate from your CustomValidator, you can erase the invocation to the validation function as you exit the textbox. Furthermore, utilizing ValidationGroups or adjusting CausesValidation = False on the remaining buttons will stop them from triggering any validation processes.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The accumulation of .ajaxComplete continues to grow

Something interesting has come to my attention. I implemented ajax in a carousel structure to dynamically add more items when needed, and it appears to be functioning correctly. Here is the ajax call located within a function named ItemLoad(): $.ajax({ ...

React Table: Conceal rows within the table and utilize a reset button to reveal the hidden rows

Within my Show column, there is a toggle button present. However, in the sandbox, it seems that the Toggle feature is not functioning properly, possibly due to tailwindcss. When clicked, this button will change the selected row to appear grayed out, giving ...

What could be causing this RangeError to constantly come up while I'm performing a basic math operation in node.js?

My program simply takes two inputs from an HTML form, parses them into text in Node, and then performs a math operation on the two. Strangely, this issue only occurs when dealing with numbers, not strings. Despite the error message indicating an invalid s ...

My Angular JS http.get request is failing to reach the specified URL

While working with AngularJS to integrate RESTful web services into my website, I am encountering an issue where I am consistently receiving errors instead of successful responses. I have been stuck on this for the past three days and any assistance would ...

Select the default option and what occurs in the event of an HTTP connection

I am currently facing an issue with two aspects. Firstly, I am struggling to set a default option in my Select element. Despite using ng-model, it only displays a blank option at the moment: <select class="form-control" ng-model="pageSize"> ...

Error: cannot use .json data with `filter` method from WEBPACK_IMPORTED_MODULE_2__["filter"]

There seems to be an error occurring when attempting to retrieve data from a JSON file in the specific line of code selectedEmployee: employeeList.data.Table[0], An issue is arising with TypeError: _employeeList_json__WEBPACK_IMPORTED_MODULE_2__.filter ...

Guide on adjusting the language settings for notifications in chosen.js?

Is it possible to customize the error message that appears in chosen.js when an unavailable option is typed into the multiple select box, which currently says 'No results match "query"'? ...

The error message "Access-Control-Allow-Origin header is missing on the requested resource" is still appearing despite having the correct CORS middleware set up

I am encountering the error message "No 'Access-Control-Allow-Origin' header is present on the requested resource" despite having implemented the necessary middleware in my express server. Here is the code snippet of the middleware placed before ...

Searching the JSON file by its value using Waterline

I am struggling with locating model instances based on the nested address attribute in one of my models. attributes: { address: { type: 'json' } } I have attempted various queries to find model instances located in the same city: Model ...

What is the best way to delete a jQuery.bind event handler that has been created for an event

I have a div and I need to assign two scroll functions to it, but I also want to remove one of them after a certain condition is met. <div id="div1" class="mydivs"> something </div> <div id="div2">Some crap here</div> <script&g ...

Is there a way to fix the issue of ContextMenu not appearing at the right position within a scrollable

I have a div within an iframe that needs to display a context menu when right-clicked. The div's length may exceed the visible area, making it scrollable. However, the issue I am facing is that the context menu appears in a different position than whe ...

Disable the ability for new windows, such as popups, submit buttons, and forms with the target set to blank, to reference

Encountering an issue, I stumbled upon some solutions here and on various other websites that I would like to share with you. The Problem: The window.opener functions of child pages stopped working after some security updates in modern browsers around 202 ...

Maximizing the potential of md-order-by attribute to organize date columns within an md-data-table

I'm attempting to implement column sorting in an AngularJS md-data-table for a column containing dates. These dates are formatted as strings DD / MM / YYYY, but I'm struggling to get them sorted correctly. I've tried calling a function that ...

When the previous textbox is filled, the cursor will automatically move to the button

Utilizing an RFID reader where students tap their ID to display basic info, a hidden button on the form opens a modal with various purposes for selection. The challenge is shifting focus of cursor to the button and automatically clicking it when the last ...

Can similar named variables be restructured or revised in some way?

const { finalScore1, finalScore2, finalScore3, finalScore4, finalScore5, finalScore6, finalScore7, finalScore8, finalScore9, finalScore10, finalScore11, finalScore12, finalScore13, finalScore14, finalScore15, finalScore16, finalScore17, fin ...

Ways to release a client-side script with npm?

Within my nodejs package, I have included code that can be executed on both the backend and in a single .js file for browsers. In order to utilize the browser script, it must be placed within a script element in an HTML file. My query pertains to whether t ...

Generating a distinct datetime string value from an array of nested arrays using JavaScript

I'm facing a challenge where I need to iterate over an array of arrays structure to filter out duplicate datetime string values and then store the unique datetime string values in a new array while ensuring they are sorted. My attempt at using a neste ...

Tips for transferring information from a controller to a directive in AngularJS using AJAX requests

I want to pass data to a directive once the call is successful. Below is the ajax call from my controller: $scope.items ={ avatar: "" }; $scope.addComment = function(segment) { commentFactory.saveComment($scope.form.comment,segment,0, ...

Angular JS Tutorial: How to Nest ng-views

When merging two separate Angular apps into one, I encountered the need to nest ng-views. The structure of my sample code (index.html) looks like this: <!doctype html> <html lang="en" ng-app="myApp"> <head> <meta ch ...

How can we smoothly animate scrolling to the top of an element using jQuery?

I am attempting to implement a function where elements scroll to the top with animation upon being clicked. However, I am facing challenges in making this work successfully. You can view my code in action here: https://jsfiddle.net/3dqzsu2m/1/ Here is ...