File: /var/www/quadcode/frontend/src/utils/ValidateInput.js
import { handle } from "./ErrorHandler";
import { langArr } from "../js/components/lp/saas2/LangArray";
const ValidateInput = (inputs) => {
if (!inputs) return;
const arrayErrors = [];
const errors = [];
let hash = localStorage.getItem("form__lang");
const validation = (item) => {
const error = document.getElementById(item.id + "-error");
switch (item.id) {
case "first_name":
if (item.value.trim() === "") {
errors.push(true);
arrayErrors[item.id] = langArr["Required"][hash];
} else {
errors.push(false);
arrayErrors[item.id] = "";
}
break;
case "region":
case "options":
case "range":
if (window.innerWidth >= 768) {
if (item.value.trim() === "") {
errors.push(true);
arrayErrors[item.id] = langArr["Required"][hash];
} else {
errors.push(false);
arrayErrors[item.id] = "";
}
}
break;
case "saas-phone":
let regExp = /^[\d\+][\d\(\)\ -]{4,14}\d$/;
const phone = item.value.trim();
if (phone === "") {
errors.push(true);
arrayErrors[item.id] = langArr["Required"][hash];
} else {
if (!regExp.test(phone) || phone.length < 10) {
errors.push(true);
error.dataset.lang = "lng-Numeric";
arrayErrors[item.id] = langArr["Numeric"][hash];
} else {
errors.push(false);
arrayErrors[item.id] = "";
error.dataset.lang = "lng-Required";
}
}
break;
case "email":
if (item.value.trim() === "") {
errors.push(true);
arrayErrors[item.id] = langArr["Required"][hash];
} else {
const regular =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (regular.test(item.value.trim())) {
errors.push(false);
arrayErrors[item.id] = "";
error.dataset.lang = "lng-Required";
} else {
errors.push(true);
error.dataset.lang = "lng-EmailInvalid";
arrayErrors[item.id] = langArr["EmailInvalid"][hash];
}
}
break;
case "message":
if (item.value.trim() === "") {
errors.push(true);
arrayErrors[item.id] = langArr["Required"][hash];
} else {
errors.push(false);
arrayErrors[item.id] = "";
}
break;
}
handle(arrayErrors);
};
if (inputs.length > 1) {
inputs.forEach((item) => {
validation(item);
});
} else {
validation(inputs);
}
return errors.includes(true);
};
export default ValidateInput;