wm-AllKids-tibi2023/api/hooks/forms/validateFields.js

81 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-09-24 11:38:03 +02:00
/**
* @param {Array<[string, FormObj]>} fieldsArray
* @returns {(string | (() => void))[][]}
*/
2023-09-17 13:24:39 +02:00
function validateFields(fieldsArray) {
2023-09-24 11:38:03 +02:00
/**@type {(string | (() => void))[][]} */
2023-09-17 13:24:39 +02:00
const errors = []
2023-09-24 11:38:03 +02:00
/**@type {number} */
2023-09-17 13:24:39 +02:00
let selectedGroup
const numberRegex = /^[+]?([.]\d+|\d+([.]\d+)?)$/
const emailRegex =
/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/
const dateRegex = /^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$/
const timeRegex = /^\d{1,2}:\d{2}-\d{1,2}:\d{2}$/
const phoneRegex = /^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/
const wholeBlockInvalid = () => {
const blockContainer = document.getElementsByClassName("blockContainer")[0]
blockContainer.classList.add("invalidBlocks")
}
2023-09-24 11:38:03 +02:00
/**
*
* @param {string} value
* @param {any} field
* @param {HTMLElement} element
*/
2023-09-17 13:24:39 +02:00
const validateNumber = (value, field, element) => {
if (!numberRegex.test(`${value}`)) {
errors.push(["block", () => element.classList.add("border-red")])
}
}
fieldsArray.forEach(([field, value]) => {
if (field === "blockGroups" || field.includes("numberLabel")) {
if (!field.includes("numberLabel")) return
2023-09-24 11:38:03 +02:00
// @ts-ignore
2023-09-17 13:24:39 +02:00
const [elementValue, element, group] = value
if (!elementValue) return
if (selectedGroup !== undefined) {
if (group !== selectedGroup) {
errors.push(["block", wholeBlockInvalid])
} else {
validateNumber(elementValue, field, element)
}
} else {
selectedGroup = group
validateNumber(elementValue, field, element)
}
return
}
if (!value) {
errors.push(["Eingabe ist erforderlich.", field])
} else if (field.includes("number_")) {
if (!numberRegex.test(`${value}`)) {
errors.push(["Ungültiger numerischer Wert.", field])
}
} else if (field.includes("agreement_")) {
if (value !== true) errors.push(["Bitte das Kontrollkästchen anklicken.", field])
} else if (field.includes("Email_")) {
if (!emailRegex.test(value)) errors.push(["Ungültiges E-Mail-Format.", field])
} else if (field.includes("date_")) {
if (!dateRegex.test(value)) errors.push(["Ungültiges Datumsformat.", field])
} else if (field.includes("times_")) {
if (!timeRegex.test(value)) errors.push(["Ungültiges Zeitformat.", field])
} else if (field.includes("Telefon_")) {
if (!phoneRegex.test(value)) errors.push(["Ungültiges Telefonnummernformat.", field])
}
})
return errors
}
module.exports = {
validateFields,
}