Skip to content

Commit

Permalink
Number fields in script UIs only accept numbers. See #10055
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Moore committed Jan 24, 2013
1 parent 92f3a07 commit 88f2f3c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,6 @@ $(document).ready(function() {
}
});

// For 'number' fields, only allow numbers input.
$(".number").numbersOnly();
});
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
$IDs.val("");
}
});

// For 'number' fields, only allow numbers input.
$(".number").numbersOnly();
});
</script>
{% endblock %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,58 @@ jQuery.fn.alternateRowColors = function() {
return this;
};


// Call this on an <input> to only allow numbers.
// I rejects all non-numeric characters but allows paste (then checks value)
// By default it only allows positive ints.
// To allow negative or float values use $(".number").numbersOnly({negative:true, float:true});
jQuery.fn.numbersOnly = function(options) {

// First, save the current value (assumed to be valid)
this.each(function() {
$(this).data('numbersOnly', $(this).val());
})
.keypress(function(event){

// we allow copy, paste, left or right
var allowedChars = [37, 39, 99, 118];
if (options && options.negative) {
allowedChars.push(45);
}
if (options && options.float) {
allowedChars.push(46);
}
// Reject keypress if not a number and NOT one of our allowed Chars
var charCode = (event.which) ? event.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57) && allowedChars.indexOf(charCode) == -1) {
return false;
}

// We've allowed keypress (including paste)...
//finally check field value after waiting for keypress to update...
var $this = $(this);
setTimeout(function(){
var n = $this.val();
var isNumber = function(n) {
if (n.length === 0) {
return true; // empty strings are allowed
}
return !isNaN(parseFloat(n)) && isFinite(n);
};
// If so, save to 'data', otherwise revert to 'data'
if (isNumber(n)) {
$this.data('numbersOnly', n); // update
} else {
$this.val( $this.data('numbersOnly') );
}
}, 10);

return true;
});

return this;
};

OME.openPopup = function(url) {
// IE8 doesn't support arbitrary text for 'name' 2nd arg. #6118
var owindow = window.open(url, '', 'height=600,width=850,left=50,top=50,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
Expand Down

0 comments on commit 88f2f3c

Please sign in to comment.