JavaScript Stop form submitting in jquery if an error stop form submitting jquery,stop form submitting on enter,stop

This is my code: http://jsfiddle.net/Xk38X/6/

  $('#register').click(function()
{
    if( $('#company_f').val().length == 0 ) {
        $('#company_f').css("border", "solid 1px red");
    }
});

The issue is even when it errors it still sends the form. Can anyone please tell me how I stop it submitting the form if the user hits the button and the company field isn't filled out.

Thank you.

Answer:1

Use return false or event.preventdefault()

$('#register').click(function (e) {
    if ($('#company_f').val().length == 0) {
        $('#company_f').css("border", "solid 1px red");
        return false; // or e.preventdefault();
    }
});


A Little better version of your code

var company_f = $('#company_f'); //cache your selector
$('#register').click(function (e) {
    if (company_f.val().length == 0) {
        company_f.css("border", "solid 1px red");
        return false; // or e.preventdefault();
    }
});

Also Read HTML required attribute

Answer:2

I'm currently working on a web application that is loaded inside an iframe inside a webview in a mobile app. As the user clicks a button, I perform an AJAX request, and once I get a successful ...

I'm currently working on a web application that is loaded inside an iframe inside a webview in a mobile app. As the user clicks a button, I perform an AJAX request, and once I get a successful ...

  1. redirect inside iframe not working
  2. redirect inside iframe
  3. redirect inside iframe javascript
  4. redirect page inside iframe
  5. php redirect inside iframe
  6. html redirect inside iframe
  7. redirect url inside iframe
  8. response.redirect inside iframe
  9. allow redirect inside iframe
  10. redirect page inside iframe javascript
  11. redirect in iframe not working

In JavaScript, defining a classlike (constructor) function method is usually done like this: function Class1() { // ... code ... } Class1.prototype.method1 = function(args) { // ... code ... }; ...

In JavaScript, defining a classlike (constructor) function method is usually done like this: function Class1() { // ... code ... } Class1.prototype.method1 = function(args) { // ... code ... }; ...

  1. using this in prototype javascript
  2. using prototype board
  3. using prototype pcb
  4. using prototype in typescript
  5. using prototype in node.js
  6. using prototype pcb board
  7. using prototype in es6
  8. using prototype js
  9. using prototype in c++
  10. using prototype inheritance in javascript
  11. using prototype angularjs
  12. using prototype in spring
  13. using prototype functions in javascript
  14. using prototype in sentences
  15. using prototype model

I'm started learning JavaScript promises, but I can't understand how apply, for example, Q functions to Node.js callback functions. In similar question is recommended to use Q.denodeify(), but it ...

I'm started learning JavaScript promises, but I can't understand how apply, for example, Q functions to Node.js callback functions. In similar question is recommended to use Q.denodeify(), but it ...

  1. using promises with axios
  2. using promises with react
  3. using promises with jquery ajax
  4. using promises with async await
  5. using promises with fetch
  6. using promises with ajax
  7. using promises with mongoose
  8. using promises with callbacks
  9. using promises with express
  10. using promises with settimeout
  11. using promises with request
  12. using promises with observables
  13. using promises with filereader
  14. using promises with mongodb
  15. using promises with jquery
  16. using promises with dynamodb
  17. using promises with async
  18. using promises with ie11
  19. using promises with node
  20. using promises in javascript

I'm having trouble replacing characters in a string. Here's the code I currently have: var entry_value = document.getElementById("entry_box").value; var length = entry_value.length; for(var l = 0; ...

I'm having trouble replacing characters in a string. Here's the code I currently have: var entry_value = document.getElementById("entry_box").value; var length = entry_value.length; for(var l = 0; ...

  1. replace character string javascript
  2. replace character string python
  3. replace character string in r
  4. replace character string c#
  5. replace character string java
  6. replace character string js
  7. replace character string powershell
  8. replace character string swift
  9. replace character string vba
  10. replace character string typescript
  11. replace character string php
  12. replace character string
  13. replace character string matlab
  14. replace character string bash
  15. replace character string excel
  16. replace character string stata
  17. replace character string sql
  18. replace character string kotlin
  19. replace character string swift 4
  20. replace character string sas