File: /var/www/quadcode-site/src/js/components/white-label-cfd-broker/Validation.js
import {handle} from "./handle";
const Validate = (inputs) => {
if (!inputs) return;
const arrayErrors = [];
const errors = [];
const validation = (item) => {
switch (item.id) {
case 'first_name':
case 'first_name-popup':
if (item.value.trim() === '') {
errors.push(true);
arrayErrors[item.id] = "Required";
} else {
errors.push(false);
arrayErrors[item.id] = '';
}
break;
case 'terms_agree':
case 'terms_agree-popup':
if (!item.checked) {
errors.push(true);
arrayErrors[item.id] = "Required";
} else {
errors.push(false);
arrayErrors[item.id] = '';
}
break;
case 'range':
case 'range-popup':
if (!Number(item.value)) {
errors.push(true);
arrayErrors[item.id] = "Required";
} else {
errors.push(false);
arrayErrors[item.id] = '';
}
break;
case 'phone':
case 'phone-popup':
let regExp = /^[\d\+][\d\(\)\ -]{4,14}\d$/;
const phone = item.value.trim();
if (phone === '') {
errors.push(true);
arrayErrors[item.id] = "Required";
} else {
if (!regExp.test(phone) || phone.length < 10) {
errors.push(true);
arrayErrors[item.id] = "Not valid phone";
} else {
errors.push(false);
arrayErrors[item.id] = '';
}
}
break;
case 'email':
case 'email-popup':
if (item.value.trim() === '') {
errors.push(true);
arrayErrors[item.id] = "Required";
} 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] = '';
} else {
errors.push(true);
arrayErrors[item.id] = "Not valid email";
}
}
break;
}
handle(arrayErrors);
};
if (inputs.length > 1 && !inputs.id) {
inputs.forEach((item) => {
validation(item);
});
} else {
validation(inputs);
}
return errors.includes(true);
};
export default Validate;