Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lessThan reports error if compared to empty field #2463

Open
enboig opened this issue Feb 2, 2023 · 3 comments
Open

lessThan reports error if compared to empty field #2463

enboig opened this issue Feb 2, 2023 · 3 comments

Comments

@enboig
Copy link

enboig commented Feb 2, 2023

Your environment

  • Version of jquery-validate: v1.19.3
  • Browser name and version: firefox 109.0.1

Current behavior

When using rule lessThan comparing to an empty field, error is triggered.

Expected behavior

Error should only be triggered if both fields are populated; user could/should add required to the other field if necessary.

Live demo

QUnit.test( "form(): with lessThan", function( assert ) {
	assert.expect( 2 );
	var form = $( "#testForm5" )[ 0 ],
		v = $( form ).validate();

	$( "#x1, #x2" ).val( "hi" );
	assert.ok( v.form(), "Valid form" );
	$( "#y2" ).val( "" );
	assert.ok( v.form(), "Valid form" );
} );
@bytestream
Copy link
Member

I'm not sure if that's a bug, and it could also be considered a breaking change if changed..

@enboig
Copy link
Author

enboig commented Feb 2, 2023

I agree it could be a breaking change...
For now I will create my custom functions lessThanIfOtherNotEmpty; next to lessThanEqualNumberIfOtherNotEmpty to compare float numbers correctly.

@enboig
Copy link
Author

enboig commented Feb 2, 2023

One question about my custom methods is if I should create a class (like does .validate-lessThanEqual-blur for each of it).

j.validator.addMethod(
    "lessThanIfOtherNotEmpty",
    function (val1, element, param) {
        var target = j(param);

        // if (
        //     this.settings.onfocusout &&
        //     target.not(".validate-lessThanIfOtherNotEmpty-blur").length
        // ) {
        //     target
        //         .addClass("validate-lessThanIfOtherNotEmpty-blur")
        //         .on("blur.validate-lessThanIfOtherNotEmpty", function () {
        //             j(element).valid();
        //         });
        // }
        var val2 = target.val(),
            normalizer;
        if ((rules = target.rules())) {
            if (typeof rules.normalizer === "function") {
                normalizer = rules.normalizer;
            } else if (typeof this.settings.normalizer === "function") {
                normalizer = this.settings.normalizer;
            }
            // If normalizer is defined, then call it to retreive the changed value instead
            // of using the real one.
            // Note that `this` in the normalizer is `element`.
            if (normalizer) {
                val2 = normalizer.call(element, val2);

                // Delete the normalizer from rules to avoid treating it as a pre-defined method.
                delete rules.normalizer;
            }
        }

        if (val1 == "" || val2 == "") {
            return true;
        }

        return val1 < val2;
    },
    "Please enter a lesser value."
);

@bytestream bytestream added this to the 2.0.0 milestone Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants