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

27 lines
925 B
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
*/
export function text_integer(id,unsigned=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)) {
// Set input value empty
if (unsigned) {
element.value = val.replace(/[^0-9]/g,'');
} else {
element.value = val.replace(/[^0-9-]/g,'').replace(/(.{1})(-)/g,'$1');
}
return false;
} else {
return true;
}
}
});
}
}