Javascript · Class · ASP.net
JS Validate class, email phone required number
A JavaScript class (Validate) with simple checks you can call before saving a form: required, email, phone, and whole number.
How to use JS Validate class, email phone required number:
After the snippet is in your project, call Validate.
Validate();phone uses optional country code then 3-3-4 digits. wholeNumber accepts digits only. An empty string fails both required and email.class Validate {
static required(value) {
return value !== null && value !== undefined && String(value).trim() !== "";
}
static email(value) {
return /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(String(value || ""));
}
static phone(value) {
return /^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/.test(String(value || ""));
}
static wholeNumber(value) {
return /^\d+$/.test(String(value || "").trim());
}
}