59 lines
2.1 KiB
JavaScript
59 lines
2.1 KiB
JavaScript
import { toastError } from "./Toast";
|
|
|
|
|
|
export function validateNumber(val, type = '', showMsg = true) {
|
|
console.log(val, "type=======", type);
|
|
let isValid = true;
|
|
if (type.toLowerCase() == 'numeric' && val != '') {
|
|
let regex = new RegExp(/^\d+$/);
|
|
let isNUmeric = regex.test(val);
|
|
if (!isNUmeric) {
|
|
isValid = false;
|
|
if (showMsg) toastError('Alert', 'Please enter whole numbers only');
|
|
}
|
|
}
|
|
else if (type.toLowerCase() == 'decimal' && val != '') {
|
|
let regex = new RegExp(/^\d*\.?\d*$/);
|
|
let isNUmeric = regex.test(val);
|
|
if (!isNUmeric) {
|
|
isValid = false;
|
|
if (showMsg) toastError('Alert', 'Please enter decimal numbers only');
|
|
}
|
|
}
|
|
else if (type.toLowerCase() == 'text' && val != '') {
|
|
let regex = new RegExp(/^[a-zA-Z0-9@\s_.-]*$/);
|
|
let isNUmeric = regex.test(val);
|
|
if (!isNUmeric) {
|
|
isValid = false;
|
|
if (showMsg) toastError('Alert', 'Please enter only characters and digits');
|
|
}
|
|
|
|
}
|
|
|
|
else if (type.toLowerCase() === 'onlytext' && val !== '') {
|
|
let regex = /^[a-zA-Z\s]*$/; // ✅ allows only alphabets and space
|
|
const isAlpha = regex.test(val);
|
|
if (!isAlpha) {
|
|
isValid = false;
|
|
if (showMsg) toastError('Alert', 'Please enter only letters and spaces');
|
|
}
|
|
}
|
|
|
|
else if (type.toLowerCase() == 'textspc' && val != '') {
|
|
let regex = new RegExp(/^[a-zA-Z0-9\/-]*$/);
|
|
let isNUmeric = regex.test(val);
|
|
if (!isNUmeric) {
|
|
isValid = false;
|
|
if (showMsg) toastError('Alert', 'Please enter only characters and digits');
|
|
}
|
|
} else if (type.toLowerCase() == 'alphanumeric' && val != '') {
|
|
let regex = new RegExp(/^[a-zA-Z0-9]+$/);
|
|
let isNUmeric = regex.test(val);
|
|
if (!isNUmeric) {
|
|
isValid = false;
|
|
if (showMsg) toastError('Alert', 'Please enter only characters and digits');
|
|
}
|
|
}
|
|
|
|
return isValid;
|
|
} |