The OnClientClick function fails to execute

I've been attempting to validate the contactno control using a JavaScript function, but it doesn't seem to be triggered before the OnClick event.

This is the JavaScript code I'm using:

<script type="text/javascript">

function validate() {
    var contactno = document.getElementById('contactno').value;
    if(contactno.toString.length != 7 && contactno.toString.length != 10) 
    {
        <% result.Text = "Please enter valid phone no !!"; %>
        return false;
    }

    else
        return true;
}
</script>

Here's the HTML snippet for this:

<asp:Button runat="server" class="loginbutton" type="submit" Text="Add" OnClientClick="return validate();" OnClick="submit_Click"></asp:Button>

During debugging, I noticed that the OnClick function gets executed without running the validate() function first.

An update on the issue:

When I begin debugging my project, I encounter an error when setting a breakpoint in the validate() function. The error message reads:

This breakpoint will not currently be hit. No executable code of the debugger's target code type is associated with this line.

Answer №1

<script type="text/javascript">

    function checkPhoneNumber() {
        var phoneNumber = document.getElementById('phoneNumber').value;
        if (phoneNumber.length != 7 && phoneNumber.length != 10) {
        <% result.Text = "Please enter a valid phone number."; %>
        return false;
    }

    else
        return true;
}
</script>

Answer №2

Attempt to achieve it without using the return keyword.

<asp:Button runat="server" class="loginbutton" type="submit" Text="Add" OnClientClick="validate()" OnClick="submit_Click"></asp:Button>

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

Deactivate DropDownList within Update Panel with JQuery

Below is the Update Panel that I am working on: <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="d ...

Creating user input fields with rich text formatting capabilities in Ruby on Rails

When dealing with rich text user input in Rails, what is considered the best approach? While Markdown seems promising, I am struggling to find a user-friendly editor for set up and unsure about how to properly sanitize the HTML. The sanitize helper still ...

Avoid API calls by using connect-history-api-fallback

I have implemented the connect-history-api-fallback along with the page.js router. page('/', index); page('/about', about); page(); function index() { console.log("viewing index"); } function about() { console.log("viewing ...

"Discovering the best method for identifying elements by their shared ID in JavaScript within a Django environment

My Django table contains all the answers for a test, but when I try to compare them with the user's response, all the answers are the same as the first one. I understand that this issue is related to getElementsById, which only selects the first eleme ...

Enhancing ASP.NET MVC: Combining multiple model items into one column

My goal for the MVC project is to display Model elements in a table format like this: SeasonTeam in a Viewbag list and iterated through each team in the season. Then, I looped through the items in the Roster Model and compared them with the SeasonTeamID ...

Generate random floating numbers at intervals and calculate their sum

I've been given a task to complete. Upon page load, there should be 10 fields labeled as A, B, C, D ... each with the initial value of 3. After the page has loaded, every 2 seconds all field values should change randomly. The change will be a rand ...

Is there a way to connect two classes based on two separate conditions in Vue.js?

I am currently developing a compact checkout stepper using Vue version 2.x.x. The item that is actively being processed should be marked with the class name "active", while all previous items should have the class name "completed", as depicted below: ...

Troubleshooting problem with AJAX returning JSON values

Utilizing a jQuery post request in the following manner: $.post('url', {data: some_data}, function(data, textStatus, jqXHR) { console.log(data); //for debugging console.log(data.status == "ok"); //for debugging .... }); The url hits ...

Utilizing NPM Script Argument Replacement

One of my npm scripts looks like this: "scripts": { "start": "webpack-dev-server --inline --config build/webpack.myconfig.js" } I want to be able to run the command with a different configuration file name instead of "myconfig", like this: npm run sta ...

Is there a way to protect the privacy of State variables within a Flux Store?

Currently, I've implemented my own version of the Flux pattern in a small scale in order to deepen my understanding of the concept. So far, it's been working well and I've been gaining valuable insights! However, I've encountered a chal ...

Is there a way to minimize the number of Nuxt pages on a website?

Consider creating a large nuxt application with 100 routes. What strategies would you recommend for effectively managing these routes within the app, excluding micro-frontend solutions? ...

Generate a custom query using user inputs

Let's say I am conducting a search in the database: router.post('/searchLoads', ensureAuthenticated, async (req, res) => { var{ agentCode, loadNumber, pickupNumber, pickupDate } = req.body}); The user does not have to complete all field ...

Synchronous execution in Node.js: Best practices for coordinating tasks

While Node.js is known for its asynchronous nature, I am seeking to perform tasks in a sequential manner as outlined below: 1. Make an API request > 2. Convert the body from XML to JSON.stringify format > 3. Pass the string to a template. request.g ...

What are the steps for logging into my asp.net application using Facebook or another form of identification?

What is the process behind websites such as SO enabling users to authenticate themselves through Facebook, OpenID, Google, Blogger, Twitter, and more? I am currently developing an ASP.NET (C#) website and I am interested in implementing a similar authenti ...

Troubleshooting problem with rotation and text geometry label on AxisHelper

Within my main scene, there is a sphere along with a subwindow located in the bottom right corner where I have displayed the (x,y,z) axis of the main scene. In this specific subwindow, I am aiming to label each axis as "X", "Y", and "Z" at the end of each ...

What are some tips for creating more concise style changes?

Is there a way to condense this code snippet? It seems too lengthy. Check out the following code block: // Function for button 1 var b1 = document.getElementById("b1"); var b2 = document.getElementById("b2"); b1.onclick = function() { updateButtonSty ...

Tips for simulating an axios request that returns an image buffer

I am attempting to simulate an axios api call that returns an image buffer as shown below: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e1 00 de 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 06 00 12 01 03 00 01 00 00 00 01 00 ... ...

Manage custom roles seamlessly with ASP.NET MVC 3's compact role management system

For a school project using asp.net mvc3, I needed user/role management but found the built-in membership system too complex. So, I am looking for an equivalent of the Zend predispatch method in ASP, or something better. My idea is to store accessible URLs ...

Angular: Truncating text based on element width - A guide

I am working on displaying a String text and determining how many words to hide based on the screen width. Here is my current approach: app.filter('words', function () { return function (input, words) { if (isNaN(words)) re ...

Hover over parts of an image to bring attention to them

I am interested in developing a webpage featuring a black and white image of 5 individuals. When hovering over each person, I would like them to illuminate and display relevant information in a dialog box next to them. Do you have any tips on how I can ac ...