2

I am new to AngularJS and try to implement form validation in "myApp" app. I wrote the code below. The {{result}} should output "true"/"false". But it didn't work.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
	<form name = "myForm">
    	<p>Input the field: </p>
        <input type = "text" name="myInput" ng-model = "myInput" required>
     </form>
        <p> The result:</p>
        <p>{{result}}</p>
        
    <script>
    	var app = angular.module("myApp", []);
        app.controller("myCtrl", function($scope){
			$scope.result = myForm.myInput.$valid;
		});
    </script>
</body>

2
  • 1
    What is your question ? Commented Sep 22, 2017 at 17:31
  • You're using a very old version of AngularJs. I would at least upgrade to the latest stable version as any responses might be incompatible. Commented Sep 22, 2017 at 17:41

2 Answers 2

2

This must be correct code for your problem. If you "watch" the changes in the "input field" then $valid result will generate.

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="MyController"> 
            <form name='myForm' novalidate>
            <p>Input the field: </p>
             <input type='text' ng-model='model.name' name='myInput' required/> 
            </form>
            <p> The result:</p>
            <p>{{myForm.myInput.$valid}}</p>
    <script> 
        var app = angular.module('myApp', []);
            app.controller('MyController',['$scope',function($scope) {
                $scope.$watch('myForm.myInput.$valid',function(newValue,oldvalue) {
                if(newValue) { 
                    //do anything with new value
                }
            });
        }]); 
    </script>
</body>
</html>

Hope this will be helpful to you.

Sign up to request clarification or add additional context in comments.

1 Comment

I get it Thank you
0

As a new User to angularJS your approcach is some how correct in form validation.In future you will learn new vesions in angular and several validation methodologies. following code line is the way that how we can check valid input in angularJS.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>  
<body ng-app="">

<p>Try writing in the input field:</p>

<form name="myForm">
<input name="myInput" ng-model="myInput" required>
</form>

<p>The input's valid state is:</p>
<h1>{{myForm.myInput.$valid}}</h1>

</body>
</html>

Your approach is correct.Keep it up and it will be great if you can define correct question clearly. :) Cheers.

2 Comments

Thanks. I also saw your provided code in W3 school which I am studying from there. My code was based on there to make some change. Basically, I want to express the validation result based on my controller. (e.g app.controller("myCtrl", function($scope){ $scope.result = myForm.myInput.$valid;) Can you point out where my code get error? Thank you
Added corrections to my previous answer after compile and testing your code. hope that will be helpful.I think without "watch" functionality controller will not get any response signal unless you use $dirty, ng-change etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.