Pressing the 'GO' button will submit the form

After developing a hybrid app using Angular and Ionic, I encountered an issue where pressing the GO button or the arrow button on Android would submit the form even if it was not valid yet. Despite attempts to prevent this by detecting keypress events and using return:false;, the problem persisted.


    <form novalidate name="questionForm" id="questionForm" ng-submit="submit()">
        <input name="question" ng-model="form.question" ng-minlength="10" required autofocus/>
        <div id="answers">
            <input ng-model="choice.choice" placeholder="Voeg antwoord toe" required ng-keypress="disableGo()" />
        </div>
        <button type="submit" ng-disabled="questionForm.$invalid" class="circle-btn btn-send ion-ios-paperplane"></button>
    </form>

Function for Keypress:


    $scope.disableGo = function () {
        var code = (event.keyCode ? event.keyCode : event.which);
        if ((code == 13) || (code == 10)) {
            return false;
        }
    };

Answer №1

When creating buttons, make sure to use type='button' rather than submit

The attribute disableGo is unnecessary in this scenario

Consider the following code snippet:

<form novalidate name="questionForm" id="questionForm">
  <input name="question" ng-model="form.question" ng-minlength="10" required autofocus/>
  <div id="answers">
    <input ng-model="choice.choice" placeholder="Add answer" required ng-keypress="disableGo()" />
  </div>
  <button type="button" ng-disabled="questionForm.$invalid" class="circle-btn btn-send ion-ios-paperplane" ng-click="submit()"></button>
</form>

For handling button clicks, use ng-click on the button element instead of ng-submit

Answer №2

Consider adding a type attribute to your button element for improved functionality

<button type="submit"></button>

Answer №3

Make sure to follow these steps:

Start by integrating a new feature into the controller:

$scope.checkValidity = function () {
        return ($scope.form.$dirty && $scope.form.$invalid) || $scope.form.$pristine
};

In your HTML file: specify type="button"

<button type="button" ng-disabled="checkValidity()" class="submit-btn icon ion-ios-checkmark"></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

Implement event listeners for multiple buttons using Handlebar.js templates, but only the final one will be active

For a homework assignment, I am developing a web app that utilizes websockets to receive server messages and then displays them on an HTML page using handlebars.js. One feature I am trying to implement is the ability to delete a message with the following ...

Why Next.js and MongoDB (minus Mongoose) is giving back an empty array

I am currently learning how to use API routes in Next.js with sample data retrieved from MongoDB. My challenge lies in attempting to retrieve data from a single object, which is proving more difficult than expected as I am new to working with MongoDB. Aft ...

Click on checkboxes to toggle selection in PHP table

I have a table that includes a checkbox in the header, intended to toggle all checkboxes below it. I've implemented a JavaScript function for the header checkbox, but it only selects the top checkbox instead of all of them. Any recommendations or guid ...

Trouble with storing data in Angular Reactive Form

During my work on a project involving reactive forms, I encountered an issue with form submission. I had implemented a 'Submit' button that should submit the form upon clicking. Additionally, there was an anchor tag that, when clicked, added new ...

In Angular, assign an error to a form control and mark it as invalid within a form group custom validator, all without the need to use

How can I set the minTemperature error for the temperature form control in a custom validator within this form group? I want it to be marked as invalid when the value is less than 26 degrees Celsius or less than 80 degrees Fahrenheit, based on the selected ...

How to trigger a Bootstrap modal using JavaScript instead of a button

Is it possible to trigger and open a bootstrap modal using JavaScript without the use of a button? I need the modal to be opened based on a different JavaScript program's logic. JAVASCRIPT: <script> //... document.getElementById("modalElem ...

"Encountering a 404 Not Found error while using Next.js and React-Query

I am currently facing a problem with setting up my Next.js project alongside an Express.js back-end. Initially, I set up the back-end as a regular one based on the documentation provided by Next.js. However, I am unsure if this approach is correct. My issu ...

Learn the steps for assigning a distribution tag to an npm package within a private registry

Operating with my own exclusive Gemfury repository, I am actively releasing npm packages. Intrigued by the prospect of applying distribution tags to my packages (as per this guide: https://docs.npmjs.com/cli/dist-tag). The configuration of my npm registr ...

Automate scrolling to the top of a webpage with Selenium WebDriver using JavaScript

I am utilizing Selenium webdriver with javascript and node.js During a certain step in my test, I need to automate clicking on a button positioned at the top of the page. However, due to some pre-treatment, the page automatically scrolls to the bottom, c ...

What is the best way to show personalized messages to users using modal windows?

Encountering bugs while trying to implement user messages in modals. function displayMessage(title, description) { var myModalErrorMessage; $("#customErrorMessageTitle").text(title) $("#customErrorMessageDescription").html(description) myModalEr ...

The color used to highlight questions on Stackoverflow

Could someone please help me identify the specific color that stackoverflow uses to highlight the background when a question answer link is followed? For instance, take a look at this link: Is there a float input type in HTML(5)? Appreciate any assistanc ...

Tips for organizing a collection of items similar to an array

I am facing a challenge with sorting a list of JSON objects based on a unix timestamp field within each object. To tackle this issue, I created a sorting function function sortUnixTimestamp(a, b){ var a = parseInt(a.timestamp); var b = parseInt(b.time ...

What is the mechanism through which the subtraction operator initiates the conversion of an array to a

Here are a couple of examples showcasing my code. Let's start with the first one: console.log([4] + 10); //"410" It is commonly known that the addition operator can only work with numbers and strings. Therefore, in this case, [4] needs to b ...

Ways to showcase additional content

When I receive a JSON Object containing posts from a WordPress account, it only includes about 15 posts. What steps can I take to retrieve more than that amount? The structure of the JSON data is as follows: { ID: 4164, title: "24 Hour Non-Stop with Ma ...

Send a pair of values using a set of two parameters

My current code is currently passing only one parameter value. However, I would like to modify it to pass two values with two parameters. This is my current code: <script> function getfilter(str){ document.getElementById("result"). ...

How to reset a form in AngularJS using either HTML or a built-in directive

Within a standard modal popup in Bootstrap, I have implemented a form consisting of input fields, a submit button, a cancel button, and a close-icon. When selecting the name from an Object data-list using ng-repeat, the popup containing the form will displ ...

Developing a unique bundle with tailored elements

I am interested in developing a custom Material UI component and making it available as a standalone package within my company's private NPM repository. Specifically, I have customized the Date Picker to create a Date Range Picker since Material UI d ...

How to dynamically generate Angular component selectors with variables or loops?

Looking to dynamically generate the Selector Tag in my app.component.html using a variable. Let's say the variable name is: componentVar:string What I want in my app.component.html: <componentVar></componentVar> or <app-componentVar& ...

Eslint is not functioning properly on the local machine

Having trouble setting up eslint for my project. When I try to run eslint --init, I keep getting this error: /usr/lib/node_modules/eslint/lib/cli.js:18 let fs = require("fs"), ^^^ SyntaxError: Unexpected strict mode reserved word at exports.runInThis ...

Ways to identify the active anchor within an li element

Below is the code snippet I am currently working with: <li> <a href="<?php echo base_url()?>home/promos/call" class="promo-call active-call"></a> </li> <li> <a href="<?php echo base_url()?>home/promos/text ...