So Im using the MEAN framework to build my web app. Like all other apps it requires a login & registration.
My Approach so far has been:
- Every major function of my app has its own AngularJS controller
- I have a data filter controller, which helps to filter data within the app. But my filter controller DOES NOT connect to the server it self. I created a AngularJS service that handles all actions with the server.
- And I have a "login controller"
The login controller is responsible for as the name suggests handling logins.
My Question:
My question then is that is carrying out Client Side form validation (i.e checking the fields are not empty, the email is of correct format etc) within a angularJS controller a good design approach?
Should I create a service that handles all the validation? Should the validation functions be private (i.e in javascript modules). Whats the best design approach for this?
Below is my login controller code:
login.controller('login', function($scope){
$scope.checkValidEmail = function(){
var elem = document.getElementById("email");
var err = document.getElementById("emailErrorMessage");
var emailLabel = document.getElementById("emailLabel");
var success = document.getElementById("emailSuccessMessage");
var email = $scope.userEmail.toLowerCase();
var atpos = email.indexOf("@");
var dotpos = email.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) {
success.style.display = "none";
err.style.display = "-webkit-inline-box";
elem.style.borderColor = "#ef4d23";
elem.style.backgroundImage = "url(./img/error_sign.png)";
elem.style.backgroundRepeat = "no-repeat";
elem.style.backgroundPosition = "325px";
elem.style.backgroundSize = "16px 15px";
emailLabel.style.display = "none";
err.style.color = "#ef4d23";
err.innerHTML = "Email address is incorrect!";
$scope.valid = true;
return false;
} else {
success.style.display = "-webkit-inline-box";
success.style.color = "#27ae60";
success.innerHTML = "Email looks great!";
err.style.display = "none";
elem.style.borderColor = "#27ae60";
elem.style.backgroundImage = "url(./img/correct_sign.png)";
elem.style.backgroundRepeat = "no-repeat";
elem.style.backgroundPosition = "325px";
elem.style.backgroundSize = "12px 16px";
$scope.valid = false;
}
};
});