Disable postback onkeypress event in the page

I am encountering an issue with my ASP.NET 4.0 form where I have multiple textboxes within divs that are dynamically shown or hidden. Specifically, I have a textbox with an onkeypress event defined as follows:

<fieldset>
    <div class="WizardStepDiv">
        <label for="MobileNumber">
            Please enter your mobile number</label>
        <input type="text" name="vmobilenumber" id="vmobilenumber" runat="server" style="width: 150px"
            onkeypress="javascript: return searchKeyPress(event);" />
    </div>
    <div class="navigationArrows">
        <div style="margin-left: 35%; width: 165px;">
            <div class="previousbutton" style="float: left; padding: 3px 0 0 6px">
                <a href="#" onclick="NavigatetoPreviousScreen()">
                    <img src="image/left_arrow.png" width="18" height="25" alt="navigateprevious" />
                </a>
            </div>
            <div class="nextbutton" style="float: right; padding: 3px 0 0 6px">
                <a href="#" id="searchMobileNumber" onclick="NavigatetoNextScreen()">
                    <img src="image/right_arrow.png" width="18" height="25" alt="navigatenext" />
                </a>
            </div>
        </div>
    </div>
</fieldset>

The searchKeyPress(event) function has a specific behavior coded as follows:

function searchKeyPress(e) {
    if (typeof e == 'undefined' && window.event) { e = window.event; }
    if (e.keyCode == 13) {
        document.getElementById('searchMobileNumber').click();
        if (e.stopPropagation) {
            e.stopPropagation();
        } else {
            e.cancelBubble = true;
        }
        (event.preventDefault) ? event.preventDefault() : event.returnValue = false;
    }
}

The NavigatetoNextScreen() function is responsible for validating the mobile number textbox. If validation passes, it triggers an ajax call as shown below:

var mobilenumber = $.trim($("#vmobilenumber").val());
if (mobilenumber == "" || !isNumber(mobilenumber)) {
    error += "<span>Please enter a valid mobile number to proceed to the next step</span></br>";
}
if (error == "") {
    $("#errormessage").hide();
    methodName = "GetVistorByMobileNumber";
    $.support.cors = true;
    $.ajax({
        type: 'GET',
        // Additional AJAX call parameters
    });

However, I am experiencing an unwanted page postback specifically in Firefox, Chrome, and IE after the ajax call returns success or error. This behavior does not occur in other browsers. Any ideas on what might be causing this issue?

Thank you for your assistance.

Answer №1

<a href="#" id="searchMobileNumber" onclick="NavigateToNewPage()">

Include the event object in the function NavigateToNewPage(event) and add the line evt.stopPropagation();

function NavigateToNewPage(){
   //... your code
   event.stopPropagation();
}

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

Setting default property values in a React component using Typescript

   Is there a way to define default property values in React components when using Typescript? I came across a post on SE suggesting the use of a static class variable called defaultProps with key-value pairs for properties. However, this method didn&a ...

The alignment of margins appears as intended in Opera, Chrome, and IE, but unfortunately it does not

I have exhausted all possible options, but I am still struggling to get this page to appear correctly in Firefox. When you click inside the square, a menu with images should display on the right side. It is properly aligned in every browser except for Fire ...

The client's secret input did not pass validation, as the provided client secret was found to be invalid during the Authorization

While utilizing the OIDC client JS in Identity Server 4 with react, I keep encountering the following error: Client secret validation failed for client, Invalid client secret during the Authorization code flow. Here are the OIDC settings: private getCl ...

What is the best way to showcase a value in JavaScript using CSS styling?

I'm looking to customize the background, font style, and outline for both open and closed elements in the code snippet below: a.innerHTML = "We are Open now now."; a.innerHTML = "We are Closed, arm."; Additionally, I want to appl ...

unable to access various attributes in an angular directive

I have a particular HTML setup that effectively outlines each attribute's value through a distinct "attachment" directive. These values are located in the attrs list of said directive, which is within a modal file upload form. <p>For Testing Pu ...

What is the best way to extract the date January 1, 1970 from a datepicker?

Currently, I am utilizing a datepicker along with a function that converts dates from dd-mm-yyyy to yyyy-mm-dd format. The dates in the database are stored in yyyy-mm-dd format, so I need to first convert them to dd-mm-yyyy for better readability. When sub ...

Nested JavaScript function expression

In this JavaScript example, the snippet provided is for educational purposes and does not include validation. After defining the variable 'isBetween' within the buildBoundDetector() function, it raises questions about how passing a number through ...

JavaScript Arrays with Four Dimensions

Looking for a solution to generate arrays with any number of dimensions, including 4D arrays. I'm interested in being able to use the function to create an array and then access it like this: arr[3][2][23][12] = "amazing"; ...

Why are my functions still being called asynchronously? I must be missing something

My task involves downloading 5 files exported from our school's database and running a query based on the first export. There will be queries for the other four files, and since there are three schools, my functions need to be scalable. I have two fu ...

Understanding the implementation of setters in JavaScript: How are they utilized in Angular controllers?

After learning about getters and setters, I came across an example that clarified things for me: var person = { firstName: 'Jimmy', lastName: 'Smith' }; Object.defineProperty(person, 'fullName', { get: function() ...

Using AngularJS to incorporate ng-include with ng-click functionality

I am trying to figure out a way to insert HTML that is specifically optimized for the controller into an alert div. Unfortunately, I have been unsuccessful so far... <script type="text/ng-include" id="login.html"> <form data-select="exepti ...

Creating a list using variables through a Post Request in Express

I am currently exploring how to create a list using a Post Request in Express. I am fetching Video Game data from an API and aiming to use this data to populate specific details within a list. For illustration: let name = localStorage.getItem("name"); let ...

Sending a POST request to Mailchimp via Express using React: A step-by-step guide

I'm currently working on a project where users can sign up for a new membership through a form in React and have their information added to the Mailchimp member list via my Express server. However, I've encountered a CORS error and am unsure if I ...

The submission of the Ajax form is malfunctioning

My attempt to load the php code without refreshing the page is failing with my Ajax code. What could be causing the issue? I prefer not to use jquery or other frameworks. Do you think the problem lies in my javascript code? Should I resort to using jquery ...

Setting the default dropdown option in Angular

For my latest question, I decided to utilize ng-init. However, upon loading the page, I noticed that there is a blank default option. When I click and select another option, the blank option disappears. How can I remove this initial blank option? Here is ...

Ensuring compatibility of peerDependencies through devDependencies in npm3

With the recent update to NPM 3, automatic resolving of peer dependencies has been removed. This poses a challenge when developing a plugin/library for consumption by another application. If the underlying library uses peerDependencies, it requires manual ...

iPad problem resolved: Disable click hover and double-click issue

I'm experiencing a problem with my web application specifically on Safari for iPad. I have to click twice in order to actually perform the click on the <a> tag. The first click triggers a hover effect, similar to when you hover with a mouse on d ...

HTML Option Absent from Blend VS2013

After recently installing VS2013 Pro in Office, I was excited to use Blend and its HTML Option. However, I am struggling to find a way to start an application with html as shown in videos. My goal was to utilize Blend for creating prototypes using html. D ...

Exploring the possibilities with JavaScript options

I am currently working on a project that involves a dropdown list... to complete unfinished sentences. Unfortunately, I am facing an issue with a message stating Uncaught TypeError: Cannot read property 'options' of null Below is a portion of m ...

Warning displayed on form input still allows submission

How can I prevent users from inserting certain words in a form on my website? Even though my code detects these words and displays a popup message, the form still submits the data once the user acknowledges the message. The strange behavior has me puzzled. ...