Prevent invalid numeric entries.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<div class="wrapper-comp-setting">
|
||||
<label class="label setting-label" for="<%= uniqueId %>"><%= model.get('display_name') %></label>
|
||||
<input class="input setting-input setting-input-number" type="number" id="<%= uniqueId %>" value='<%= model.get("value") %>' onkeyup="checkNumberValidity(this)"/>
|
||||
<input class="input setting-input setting-input-number" type="number" id="<%= uniqueId %>" value='<%= model.get("value") %>'/>
|
||||
<button class="action setting-clear inactive" type="button" name="setting-clear" value="Clear" data-tooltip="Clear">
|
||||
<i class="ss-icon ss-symbolicons-block undo">↩</i><span class="sr">Clear Value</span>
|
||||
</button>
|
||||
|
||||
@@ -897,9 +897,5 @@ function saveSetSectionScheduleDate(e) {
|
||||
});
|
||||
}
|
||||
|
||||
function checkNumberValidity(e) {
|
||||
//e.preventDefault;
|
||||
//$(e).val($(e).val().match(/\d*\.?\d+/));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -150,7 +150,8 @@ CMS.Views.Metadata.Number = CMS.Views.Metadata.AbstractEditor.extend({
|
||||
|
||||
events : {
|
||||
"change input" : "updateModel",
|
||||
"keypress .setting-input" : "showClearButton" ,
|
||||
"keypress .setting-input" : "keyPressed",
|
||||
"change .setting-input" : "changed",
|
||||
"click .setting-clear" : "clear"
|
||||
},
|
||||
|
||||
@@ -162,14 +163,17 @@ CMS.Views.Metadata.Number = CMS.Views.Metadata.AbstractEditor.extend({
|
||||
var step = "step";
|
||||
var options = this.model.getOptions();
|
||||
if (options.hasOwnProperty(min)) {
|
||||
this.$el.find('input').attr(min, options[min].toString());
|
||||
this.min = Number(options[min]);
|
||||
this.$el.find('input').attr(min, this.min.toFixed(4));
|
||||
}
|
||||
if (options.hasOwnProperty(max)) {
|
||||
this.$el.find('input').attr(max, options[max].toString());
|
||||
this.max = Number(options[max]);
|
||||
this.$el.find('input').attr(max, this.max.toFixed(4));
|
||||
}
|
||||
var stepValue = undefined;
|
||||
if (options.hasOwnProperty(step)) {
|
||||
stepValue = options[step].toString();
|
||||
// Parse step and convert to String. Polyfill doesn't like float values like ".1" (expects "0.1").
|
||||
stepValue = Number(options[step]).toFixed(4);
|
||||
}
|
||||
else if (this.model.getType() === 'Integer') {
|
||||
stepValue = "1";
|
||||
@@ -195,7 +199,35 @@ CMS.Views.Metadata.Number = CMS.Views.Metadata.AbstractEditor.extend({
|
||||
|
||||
setValueInEditor : function (value) {
|
||||
this.$el.find('input').val(value);
|
||||
},
|
||||
|
||||
keyPressed: function (e) {
|
||||
this.showClearButton();
|
||||
// This first filtering if statement is take from polyfill to prevent
|
||||
// non-numeric input (for browsers that don't use polyfill because they DO have a number input type).
|
||||
var _ref, _ref1;
|
||||
if (((_ref = e.keyCode) !== 8 && _ref !== 9 && _ref !== 35 && _ref !== 36 && _ref !== 37 && _ref !== 39) &&
|
||||
((_ref1 = e.which) !== 45 && _ref1 !== 46 && _ref1 !== 48 && _ref1 !== 49 && _ref1 !== 50 && _ref1 !== 51
|
||||
&& _ref1 !== 52 && _ref1 !== 53 && _ref1 !== 54 && _ref1 !== 55 && _ref1 !== 56 && _ref1 !== 57)) {
|
||||
e.preventDefault();
|
||||
}
|
||||
// For integers, prevent decimal points.
|
||||
if (this.model.getType() === 'Integer' && e.keyCode === 46) {
|
||||
e.preventDefault();
|
||||
}
|
||||
},
|
||||
|
||||
changed: function () {
|
||||
// Limit value to the range specified by min and max (necessary for browsers that aren't using polyfill).
|
||||
var value = this.getValueFromEditor();
|
||||
if ((this.max !== undefined) && value > this.max) {
|
||||
value = this.max;
|
||||
} else if ((this.min != undefined) && value < this.min) {
|
||||
value = this.min;
|
||||
}
|
||||
this.setValueInEditor(value);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ class CapaFields(object):
|
||||
seed = StringyInteger(help="Random seed for this student", scope=Scope.user_state)
|
||||
weight = StringyFloat(display_name="Problem Weight",
|
||||
help="Specifies the number of points the problem is worth. If unset, each response field in the problem is worth one point.",
|
||||
values = {"min" : 0 , "step": ".1"},
|
||||
values = {"min" : 0 , "step": .1},
|
||||
scope=Scope.settings)
|
||||
markdown = String(help="Markdown source of this module", scope=Scope.settings)
|
||||
source_code = String(help="Source code for LaTeX and Word problems. This feature is not well-supported.",
|
||||
|
||||
Reference in New Issue
Block a user