1

This is my code:

$('.btn-back').off('click').on('click', function (event) {
    var valid = carInfo.validate();
    if (valid) {
        event.preventDefault();
    }
});

the sentence event.preventDefault() always executes no matter what value valid is.

When I change the code to

$('.btn-back').off('click').on('click', function (event) {
    var valid = carInfo.validate();
    if (valid) {
        event.preventDefault();
    } else {

    }
});

it runs the way it should be. Maybe it's something related to Automatic Semicolon Insertion (ASI).

Sparky
  • 94,381
  • 25
  • 183
  • 265
renyalvarado
  • 169
  • 4
  • 5
    So what is `validate` function? – u_mulder Jan 07 '15 at 20:49
  • possible duplicate of [boolean in an if statement](http://stackoverflow.com/questions/15393935/boolean-in-an-if-statement) – Kritner Jan 07 '15 at 20:50
  • 2
    What does `validate()` return? It could be working on a 'truthy' value. – Justin Wood Jan 07 '15 at 20:50
  • 1
    it won't execute if `valid` isn't true, it's more likely that your function doesn't return what you expect – charlietfl Jan 07 '15 at 20:51
  • Did you debug the code or had a break point on the preventDefault()? I can't believe that the first code block is not getting to preventDefault() and the second block get to it – Amr Elgarhy Jan 07 '15 at 20:54
  • `validate()` is my own method; right now, it returns false. When I change the code to `if (false) { event.preventDefault(); }`, the sentence event.preventDefault() always executes – renyalvarado Jan 07 '15 at 21:04
  • Update! If I add a sentence after the "if", it works! `if (valid) { event.preventDefault(); } console.log('Hello World!');` I don't know why, but `if (valid) { event.preventDefault(); }` can't be the last sentence in the function. – renyalvarado Jan 07 '15 at 21:18

1 Answers1

5

Use the jQuery Validation plugin way:

carInfo.validate() will return Validator object.

So valid variable is always true. Use valid method from docs:

$('.btn-back').off('click').on('click', function (event) {
    carInfo.validate();
    if (carInfo.valid()) {
        event.preventDefault();
    }
});
Pinal
  • 10,391
  • 12
  • 45
  • 62
  • 1
    Do you need to call `validate()`? Don't you just need `if (carInfo.valid()) {`? – Rocket Hazmat Jan 07 '15 at 20:58
  • `validate()` needs to be called on the form before checking it using this method. From [docs](http://jqueryvalidation.org/valid) :) – Pinal Jan 07 '15 at 21:00
  • Ok, was just making sure :-) – Rocket Hazmat Jan 07 '15 at 21:00
  • [As per OP's comment, _"`validate()` is my own method"_](http://stackoverflow.com/questions/27828360/why-does-this-if-always-execute-even-the-condition-is-false#comment44063167_27828360), this question is **not** about the jQuery Validate plugin. – Sparky Jan 07 '15 at 21:06
  • I added a proposal to use jQuery validate :) – Pinal Jan 07 '15 at 21:08
  • Even if it was, you would not call `.validate()` from within a `click` handler. It's only how the plugin is _initialized_. – Sparky Jan 07 '15 at 21:09