8000 Core: Add greaterThan and lessThan methods. by RobJohnston · Pull Request #2061 · jquery-validation/jquery-validation · GitHub
[go: up one dir, main page]

Skip to content

Core: Add greaterThan and lessThan methods. #2061

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to 8000 load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/additional/greaterThan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$.validator.addMethod( "greaterThan", function( value, element, param ) {
var target = $( param );

if ( this.settings.onfocusout && target.not( ".validate-greaterThan-blur" ).length ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this focus handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My motivation was to follow the pattern of the equalTo method, and this was introduced there two years ago in Core: Bind the blur event just once in equalTo rule .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a clarification, the focus handling was added +5 years ago in this commit aca144b#diff-a692042a498ec7e201f38855d169e7b1R1163. The only thing that was added in the commit linked by @RobJohnston is binding the blur event one time.

target.addClass( "validate-greaterThan-blur" ).on( "blur.validate-greaterThan", function() {
$( element ).valid();
} );
}

return value > target.val();
}, "Please enter a greater value." );
11 changes: 11 additions & 0 deletions src/additional/greaterThanEqual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$.validator.addMethod( "greaterThanEqual", 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();
} );
}

return value >= target.val();
}, "Please enter a greater value." );
11 changes: 11 additions & 0 deletions src/additional/lessThan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$.validator.addMethod( "lessThan", 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();
} );
}

return value < target.val();
}, "Please enter a lesser value." );
11 changes: 11 additions & 0 deletions src/additional/lessThanEqual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$.validator.addMethod( "lessThanEqual", 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();
} );
}

return value <= target.val();
}, "Please enter a lesser value." );
14 changes: 13 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,19 @@ $.extend( $.validator, {
.removeData( "validator" )
.find( ".validate-equalTo-blur" )
.off( ".validate-equalTo" )
.removeClass( "validate-equalTo-blur" );
.removeClass( "validate-equalTo-blur" )
.find( ".validate-lessThan-blur" )
.off( ".validate-lessThan" )
.removeClass( "validate-lessThan-blur" )
.find( ".validate-lessThanEqual-blur" )
.off( ".validate-lessThanEqual" )
.removeClass( "validate-lessThanEqual-blur" )
.find( ".validate-greaterThanEqual-blur" )
.off( ".validate-greaterThanEqual" )
.removeClass( "validate-greaterThanEqual-blur" )
.find( ".validate-greaterThan-blur" )
.off( ".validate-greaterThan" )
.removeClass( "validate-greaterThan-blur" );
}

},
Expand Down
6 changes: 5 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ <h2 id="qunit-userAgent"></h2>
<form id="testForm5">
<input data-rule-equalto="#x2" value="x" name="x1" id="x1">
<input data-rule-equalto="#x1" value="y" name="x2" id="x2">
</form>
<input data-rule-lessThan="#y2" value="1" name="y1" id="y1">
<input data-rule-greaterThan="#y1" value="10" name="y2" id="y2">
<input data-rule-lessThanEqual="#z2" value="10" name="z1" id="z1">
<input data-rule-greaterThanEqual="#z1" value="10" name="z2" id="z2">
</form>
<form id="testForm6">
<input data-rule-required="true" data-rule-minlength="2" type="checkbox" name="check" id="form6check1">
<input type="checkbox" name="check" id="form6check2">
Expand Down
40 changes: 40 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,26 @@ QUnit.test( "#1760 - step modulo/remainder regression tests", function( assert )
}
} );

QUnit.test( "lessThan", function( assert ) {
var v = jQuery( "#form" ).validate(),
method = $.validator.methods.lessThan,
e = $( "#value2" );

assert.ok( method.call( v, "1", e[ 0 ], "#value2" ), "Valid integer" );
assert.ok( !method.call( v, "10", e[ 0 ], "#value2" ), "Invalid integer" );
assert.ok( !method.call( v, "11", e[ 0 ], "#value2" ), "Invalid integer" );
} );

QUnit.test( "lessThanEqual", function( assert ) {
var v = jQuery( "#form" ).validate(),
method = $.validator.methods.lessThanEqual,
e = $( "#value2" );

assert.ok( method.call( v, "1", e[ 0 ], "#value2" ), "Valid integer" );
assert.ok( method.call( v, "10", e[ 0 ], "#value2" ), "Valid integer" );
assert.ok( !method.call( v, "11", e[ 0 ], "#value2" ), "Invalid integer" );
} );

QUnit.test( "equalTo", function( assert ) {
var v = jQuery( "#form" ).validate(),
method = $.validator.methods.equalTo,
Expand All @@ -388,6 +408,26 @@ QUnit.test( "equalTo", function( assert ) {
assert.ok( method.call( v, "T", e[ 1 ], "#text2" ), "Another one" );
} );

QUnit.test( "greaterThanEqual", function( assert ) {
var v = jQuery( "#form" ).validate(),
method = $.validator.methods.greaterThanEqual,
e = $( "#value2" );

assert.ok( !method.call( v, "1", e[ 0 ], "#value2" ), "Invalid integer" );
assert.ok( method.call( v, "10", e[ 0 ], "#value2" ), "Valid integer" );
assert.ok( method.call( v, "11", e[ 0 ], "#value2" ), "Valid integer" );
} );

QUnit.test( "greaterThan", function( assert ) {
var v = jQuery( "#form" ).validate(),
method = $.validator.methods.greaterThan,
e = $( "#value2" );

assert.ok( !method.call( v, "1", e[ 0 ], "#value2" ), "Invalid integer" );
assert.ok( !method.call( v, "10", e[ 0 ], "#value2" ), "Invalid integer" );
assert.ok( method.call( v, "11", e[ 0 ], "#value2" ), "Valid integer" );
} );

QUnit.test( "extension", function( assert ) {
var method = methodTest( "extension" ),
v;
Expand Down
13 changes: 12 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,7 @@ QUnit.test( "Validation triggered on radio and checkbox via click", function( as
} );

QUnit.test( "destroy()", function( assert ) {
assert.expect( 6 );
assert.expect( 14 );

var form = $( "#testForm5" ),
validate = form.validate();
Expand All @@ -2451,10 +2451,21 @@ QUnit.test( "destroy()", function( assert ) {
assert.equal( $( "#x1", form ).hasClass( "validate-equalTo-blur" ), true, "The blur event should be bound to this element" );
assert.equal( $( "#x2", form ).hasClass( "validate-equalTo-blur" ), true, "The blur event should be bound to this element" );

assert.equal( $( "#y1", form ).hasClass( "validate-greaterThan-blur" ), true, "The blur event should be bound to this element" );
assert.equal( $( "#y2", form ).hasClass( "validate-lessThan-blur" ), true, "The blur event should be bound to this element" );
assert.equal( $( "#z1", form ).hasClass( "validate-greaterThanEqual-blur" ), true, "The blur event should be bound to this element" );
assert.equal( $( "#z2", form ).hasClass( "validate-lessThanEqual-blur" ), true, "The blur event should be bound to this element" );

validate.destroy();
assert.strictEqual( form.data( "validator" ), undefined );
assert.equal( $( "#x1", form ).hasClass( "validate-equalTo-blur" ), false, "The blur event should be unbound from this element" );
assert.equal( $( "#x2", form ).hasClass( "validate-equalTo-blur" ), false, "The blur event should be unbound from this element" );

assert.equal( $( "#y1", form ).hasClass( "validate-lessThan-blur" ), false, "The blur event should be unbound from this element" );
assert.equal( $( "#y2", form ).hasClass( "validate-greaterThan-blur" ), false, "The blur event should be unbound from this element" );
assert.equal( $( "#z1", form ).hasClass( "validate-equalTo-blur" ), false, "The blur event should be unbound from this element" );
assert.equal( $( "#z2", form ).hasClass( "validate-greaterThan-blur" ), false, "The blur event should be unbound from this element" );

} );

QUnit.test( "#1618: Errorlist containing more errors than it should", function( assert ) {
Expand Down
0