Last active
December 28, 2017 09:43
-
-
Save dsherret/50d1a6ee93c8672da782 to your computer and use it in GitHub Desktop.
Limit input by regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* Copyright 2014 - David Sherret | |
* MIT License | |
*/ | |
// Limits an input to specific values without checking keycodes and without flickering the value. | |
// | |
// Example usage: | |
// $("input").limitRegex(/^[0-9]+\.?[0-9]{0,2}$/); -- limit to numbers, up to 2 decimal places | |
// | |
// http://jsfiddle.net/f1w38jw0/15/ | |
(function($) { | |
$.fn.limitRegex = function(regex, onFail) { | |
var pastValue, pastSelectionStart, pastSelectionEnd; | |
$.each(this, function() { | |
$(this).on("keydown", function() { | |
pastValue = this.value; | |
pastSelectionStart = this.selectionStart; | |
pastSelectionEnd = this.selectionEnd; | |
}); | |
$(this).on("input propertychange", function() { | |
if (this.value.length > 0 && !regex.test(this.value)) { | |
if (typeof onFail === "function") { | |
onFail.call(this, this.value, pastValue); | |
} | |
this.value = pastValue; | |
this.selectionStart = pastSelectionStart; | |
this.selectionEnd = pastSelectionEnd; | |
} | |
}); | |
}); | |
return this; | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment