Skip to content

Commit

Permalink
Added validation for values with leading zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsharman committed Feb 26, 2024
1 parent 2b8bc99 commit c95847f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/module-Authoring_Widgets.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ <h4 class="name" id=".type">type<span class="signature">()</span><span class="ty


<p>The <code>type</code> of widget currently being edited. Returns
undefined if not on the widget edit screen.</p>
<code>undefined</code> if not on the widget edit screen.</p>

<dl class="details">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ <h1 class="page-title">_Extensions/Authoring/validation/essayMaxLength</h1>
<p>Will be changed to:</p>
<pre class="prettyprint source"><code>10
</code></pre>
<p>If the value contains leading zeros, the input field
will be styled with a red border.</p>

<dl class="details">

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@caspingus/lt",
"version": "2.4.0",
"version": "2.5.0",
"description": "A utility library of helpers and tools for working with Learnosity APIs.",
"main": "src/index.js",
"author": "michael@learnosity.com",
Expand Down
39 changes: 39 additions & 0 deletions src/authoring/extensions/validation/essayMaxLength/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ import * as widgets from '../../../core/widgets';
* 10
* ```
*
* If the value contains leading zeros, the input field
* will be styled with a red border.
*
* @module _Extensions/Authoring/validation/essayMaxLength
*/

const state = {
renderedCss: false,
validTypes: ['longtextV2', 'plaintext'],
};

Expand All @@ -37,6 +41,7 @@ const state = {
* @since 2.4.0
*/
export function run() {
if (!state.renderedCss) injectCSS();
setupListeners();
}

Expand Down Expand Up @@ -69,14 +74,48 @@ function setupListeners() {
* Inspects the value of the passed DOM element. If the
* value contains non-numeric values (including period)
* those invalid characters are removed.
* If the value contains leading zero(s), the input field
* is styled with a red border indicating that this is
* an invalid value.
* @since 2.4.0
* @param {*} el
* @ignore
*/
function validateInput(el) {
const regex = /^\d+$/;
const leadingZeroRegEx = /^(0|[1-9]\d*)$/;
const invalidClass = 'lt__input-invalid';

if (!regex.test(el.value)) {
el.value = el.value.replace(/[^0-9]/g, '');
}

if (el.value.length && !leadingZeroRegEx.test(el.value)) {
el.classList.add(invalidClass);
} else {
el.classList.remove(invalidClass);
}
}

/**
* Injects the necessary CSS to the header
* @since 2.5.0
* @ignore
*/
function injectCSS() {
const elStyle = document.createElement('style');
const css = `
/* Learnosity essay validate max length styles */
.lrn-qe-ui .lrn-qe-form-group .lrn-qe-form-control.lt__input-invalid,
.lrn-qe-ui .lrn-qe-form-group .lrn-qe-form-control.lt__input-invalid:active:not(:disabled):not([readonly]),
.lrn-qe-ui .lrn-qe-form-group .lrn-qe-form-control.lt__input-invalid:focus:not(:disabled):not([readonly]) {
border-color: #ff0000;
outline: 0.0714285714em solid #dd002f;
}
`;

elStyle.textContent = css;
document.head.append(elStyle);

state.renderedCss = true;
}

0 comments on commit c95847f

Please sign in to comment.