frontend: level validation

This commit is contained in:
realaravinth 2021-05-01 21:27:02 +05:30
parent bc749c387b
commit 191e9658ec
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
2 changed files with 46 additions and 7 deletions

View file

@ -16,6 +16,7 @@
*/
import VALIDATE_LEVELS from './levels';
import isBlankString from '../../utils/isBlankString';
import isNumber from '../../utils/isNumber';
const LABEL_CONTAINER_CLASS = 'sitekey-form__add-level-flex-container';
const LABEL_CLASS = 'sitekey-form__label';
@ -38,20 +39,33 @@ const validateLevel = (numLevels: number) => {
let inputID = INPUT_ID_WITHOUT_LEVEL + numLevels.toString();
let filed = LABEL_INNER_TEXT_WITHOUT_LEVEL + numLevels;
let inputElement = <HTMLInputElement>document.getElementById(inputID);
let val = inputElement.value;
if (!isNumber(val)) {
return false;
}
let level = parseInt(val);
if (Number.isNaN(level)) {
alert('Level can contain nubers only');
return false;
}
let e = null;
console.log(level);
isBlankString(e, val, filed);
let isValid = VALIDATE_LEVELS.add(parseInt(val));
let isValid = VALIDATE_LEVELS.add(level);
return isValid;
};
const addLevelButtonEventHandler = (e: Event) => {
let eventTarget = <HTMLElement>e.target;
// if (!eventTarget) {
// return;
// }
let eventTarget = <HTMLElement>e.target;
// if (!eventTarget) {
// return;
// }
const PREV_LEVEL_CONTAINER = <HTMLElement>eventTarget.parentElement;
let numLevels: string|number = getNumLevels();
let numLevels: string | number = getNumLevels();
let isValid = validateLevel(numLevels);
console.log(`[addLevelButton] isValid: ${isValid}`);
@ -99,7 +113,9 @@ const addLevelButtonEventHandler = (e: Event) => {
};
export const addLevelButtonAddEventListener = () => {
let addLevelButton = <HTMLElement>document.querySelector(`.${ADD_LEVEL_BUTTON}`);
let addLevelButton = <HTMLElement>(
document.querySelector(`.${ADD_LEVEL_BUTTON}`)
);
addLevelButton.addEventListener('click', addLevelButtonEventHandler);
};

View file

@ -0,0 +1,23 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const isNumber = (value: string) => {
value = value.toString();
return /^\d+$/.test(value);
};
export default isNumber;