This repository has been archived on 2025-01-01. You can view files and clone it, but cannot push or open issues or pull requests.
moodle-local_treestudyplan/amd/src/util/formfields.js

31 lines
1.1 KiB
JavaScript
Raw Normal View History

/**
* convert a text field into an integer only text field
* @param {string} id The Id of the form field
* @param {bool} unsigned Allow only unsigned values
* @param {bool} nonzero Do not allow zero values
*/
export function text_integer(id,unsigned=false,nonzero=false){
const element = document.getElementById(id);
if (element) {
element.addEventListener("input",() => {
var val = element.value;
if (val != '') {
if ((isNaN(val) && !(!unsigned && val == '-')) || (unsigned && val < 0) || (nonzero && val == 0)) {
// Set input value empty
if (unsigned) {
element.value = val.replace(/[^0-9]/g,'');
if (nonzero && val == 0) {
element.value = '';
}
} else {
element.value = val.replace(/[^0-9-]/g,'').replace(/(.{1})(-)/g,'$1');
}
return false;
} else {
return true;
}
}
});
}
}