LessThan and LessThanEqual methods don't work correctly for negative numbers · Issue #2164 · jquery-validation/jquery-validation · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

LessThan and LessThanEqual methods don't work correctly for negative numbers #2164

Open
@Laura-Guillory

Description

@Laura-Guillory

Subject of the issue

When negative numbers are entered in an input field with a lessThan or lessThanEqual rule applied, the form will pass validation when it shouldn't, and fail validation when it should pass - as if the rule was actually greaterThan. What seems to be happening is that the absolute value is used (-1 is treated as 1, for example). So if we have -2 compared to -1, -1 is treated as being "less than" -2, even though -2 < -1.

Your environment

  • version of jquery-validate: Current repository, not released yet
  • which browser and its version: Firefox 60.0b11, also tested on Chrome 65.0.3325.181

Steps to reproduce

lessThan with negative numbers:

  1. Open the following HTML file
  2. Enter -2 and -1 in the two input fields respectively
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <form id="form">
        <input id="A" name="A" type="number" value="" /><br />
        <input id="B" name="B" type="number" value="" /><br />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://github.com/jquery-validation/jquery.validate.js"></script>
<script src="https://github.com/jquery-validation/additional-methods.js"></script>
<script>
    $("#form").validate({
        rules: {
            A: {
                lessThan: "#B"
            }
        }
    });
</script>

lessThanEqual with negative numbers:

  1. Open the following HTML file
  2. Enter -2 and -1 in the two input fields respectively
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <form id="form">
        <input id="A" name="A" type="number" value="" /><br />
        <input id="B" name="B" type="number" value="" /><br />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://github.com/jquery-validation/jquery.validate.js"></script>
<script src="https://github.com/jquery-validation/additional-methods.js"></script>
<script>
    $("#form").validate({
        rules: {
            A: {
                lessThanEquals: "#B"
            }
        }
    });
</script>

Expected behaviour

In both instances, I would expect validation to pass, since -2 < -1.

Actual behaviour

Validation fails with the message: "Please enter a lesser value."
If the numbers are swapped, validation will pass which is incorrect.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      TMZ Celebrity News – Breaking Stories, Videos & Gossip

      Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

      Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

      🎥 Watch TMZ Live

      TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.