-3

I want to know to how to validate a form in angularjs using controllers?

en$scope.saveDetail = function () {

    if ($scope.register.Name == "") {
        alert("Pls fill register Name");
        return false;
    }
    if ($scope.register.Address == "") {
        alert("Pls fill Address");
        return false;
    }
    if ($scope.register.Email == "") {
        alert("Pls fill Email");
        return false;
    }
    if ($scope.register.Password == "") {
        alert("Pls fill Password");
        return false;
    }
    if ($scope.register.Number == "") {
        alert("Pls fill 10 digit Number");
        return false;
    }

    $localStorage.myRegisterDetail = $scope.register;
    alert("Your records save successfully");
    $state.go("app.login");

};

Now i want to add code for setting minimum and maximum length for password.How do I do that?

  • This question is really too general... A simple search on Google would have shown you this: https://docs.angularjs.org/guide/forms... Try to be more specific next time. – ssougnez Mar 02 '17 at 08:07
  • @ssougnez is right. This is one of the most common angular use cases. Search 'angular Form validation' on Google or here on SO. By the way, Wolverine doesn't suck! – Ladmerc Mar 02 '17 at 10:20

1 Answers1

0

In case you want to use custom validations using basic functionality I suggest you use JQuery/Javascript for the same in an angular function.

Note:- This is an example for angular1. I have not yet read much about angular2 and thus the answer may vary.

Sample solution:-

en$scope.saveDetail = function () {
    var validationError = false;
    if ($scope.register.Name == "") {
        alert("Pls fill register Name");
        validationError = true;
    }

    if(!validationError) {
        if ($scope.register.Address == "") {
            alert("Pls fill Address");
            validationError = true;
        }
    }
    if(!validationError) {
        if ($scope.register.Email == "") {
            alert("Pls fill Email");
            validationError = true;
        }
    }

    if(!validationError) {
        if ($scope.register.Password == "") {
            alert("Pls fill Password");
            validationError = true;
        }
    }  

    if(!validationError) {
        if ($scope.register.Number == "") {
            alert("Pls fill 10 digit Number");
            validationError = true;
        }
    }    

    if(!validationError) {
        $localStorage.myRegisterDetail = $scope.register;
        alert("Your records save successfully");
        $state.go("app.login");
    }
};
Sagar
  • 477
  • 4
  • 15
  • @WolverineSucks Not sure if it helps but here's a similar question [http://stackoverflow.com/questions/30049938/angularjs-controller-as-and-form-valid?rq=1] – Sagar Mar 02 '17 at 08:26
  • Hi. Sorry but I had a small error on my code. Also please do submit your code so we could try and find the bug in it. – Sagar Mar 02 '17 at 08:40
  • Ah sorry. I did not notice the same. For the answer Angular does not take in return statement in the function the way it does for vanilla javascript and jqeury. It will continue to execute all the codes. I will modify my code for the same and help you with it. – Sagar Mar 02 '17 at 08:45
  • thank you so much for the code. But what about adding validation for max and min length for password? – Wolverine Sucks Mar 02 '17 at 09:42