I am trying to come up with one function that can handle if statements in my code to avoid repetition of the same. For example in the code below
// email
App.Cmp.form.ValidateEmail = function(email) {
var MyEmailId = document.getElementById(email);
var format = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
if (!MyEmailId.value.match(format)) {
this.displayWarning(email, MyEmailId.parentNode.id, "Invalid email!");
} else {
removeWarning(email, MyEmailId.parentNode.id);
}
}
// password
App.Cmp.form.ValidatePassword = function(password) {
var MyPasswordId = document.getElementById(password);
var format = /(?=^.{8,}$)(?=.*\d)(?=.*[!@#$%^&*]+)(?![.\n])(?=.*[A-Z])(?=.*[A-Z]).*$/;
if (!MyPasswordId.value.match(format)) {
this
.displayWarning(
password,
MyPasswordId.parentNode.id,
"The password must have uppercase, lowercase, numeric and special characters and at least 8 characters long");
} else {
this.removeWarning(password, MyPasswordId.parentNode.id);
}
}