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

greaterThan() not normalizing input #2450

Open
enboig opened this issue Oct 26, 2022 · 2 comments
Open

greaterThan() not normalizing input #2450

enboig opened this issue Oct 26, 2022 · 2 comments

Comments

@enboig
Copy link

enboig commented Oct 26, 2022

Your environment

  • Version of jquery-validate: v1.19.5
  • Browser name and version: Firefox

Current behavior

I need to compare 2 datetime fields. They are localized so I have writen a normalizer to transform them to "YYYY-MM-DD hh:mm" form (including leading zeros).
When I compare them with greaterThan() it fails because second date is not normalized

Expected behavior

If input field has a normalizer, use it before comparing.

https://jsbin.com/goruriroto/edit?html,js,console,output

It may also affect greaterThanEqual, lessThan and lessThanEqual

@enboig
Copy link
Author

enboig commented Oct 26, 2022

It's the same as #1964

@enboig
Copy link
Author

enboig commented Nov 4, 2022

After using a "normalization" it ended in:

  • If no normalization is used, "10" < "5" => true
  • If normalization is used to return a Float, method required fails because (10).length == 0
    So I ended creating my own methods:
$.validator.addMethod( "greaterThanNumber", function( value, element, param ) {
    var target = $( param );

    if ( this.settings.onfocusout && target.not( ".validate-greaterThan-blur" ).length ) {
        target.addClass( "validate-greaterThan-blur" ).on( "blur.validate-greaterThan", function() {
            $( element ).valid();
        } );
    }
	var val = 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 ) {
			val = normalizer.call( element, val );

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

	}
    return Number.parseFloat(value) > Number.parseFloat(val);
}, "Please enter a greater value." );

$.validator.addMethod( "greaterThanEqualNumber", function( value, element, param ) {
    var target = $( param );

    if ( this.settings.onfocusout && target.not( ".validate-greaterThanEqual-blur" ).length ) {
        target.addClass( "validate-greaterThanEqual-blur" ).on( "blur.validate-greaterThanEqual", function() {
            $( element ).valid();
        } );
    }

	var val = 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 ) {
			val = normalizer.call( element, val );
		}
	}
	return Number.parseFloat(value) >= Number.parseFloat(val);
}, "Please enter a greater value." );

$.validator.addMethod( "lessThanNumber", function( value, element, param ) {
    var target = $( param );

    if ( this.settings.onfocusout && target.not( ".validate-lessThan-blur" ).length ) {
        target.addClass( "validate-lessThan-blur" ).on( "blur.validate-lessThan", function() {
            $( element ).valid();
        } );
    }
	var val = 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 ) {
			val = normalizer.call( element, val );

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

	}
    return Number.parseFloat(value) < Number.parseFloat(val);
}, "Please enter a lesser value." );

$.validator.addMethod( "lessThanEqualNumber", function( value, element, param ) {
    var target = $( param );

    if ( this.settings.onfocusout && target.not( ".validate-lessThanEqual-blur" ).length ) {
        target.addClass( "validate-lessThanEqual-blur" ).on( "blur.validate-lessThanEqual", function() {
            $( element ).valid();
        } );
    }

	var val = 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 ) {
			val = normalizer.call( element, val );
		}
	}
    return Number.parseFloat(value) <= Number.parseFloat(val);
}, "Please enter a lesser value." );

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