简体   繁体   中英

AngularJS Forms, check validity

I have a form and want to test the validity of the form. I have a function in the controller that checks for $valid

$scope.checkValid = function(){
    console.log($scope.form.address)
    console.log($scope.form.address.$valid);
    if($scope.form.address.$valid){ 
       dosomething();
     }
 }   

The dosomething() does nothing!! The first console.log produces a Constructor object with the valid as true.

$valid: true

yet the second console log displays FALSE!

Why would this happen? I cant figure it out and google has no answers.

Any advice appreciated.

You want to validate form validity, and thats variable "formName.$valid" in form´s scope.

You really should provide jsfiddle as others mentioned, because you didnt specified, where and how are you using this method checkValid();

You can see any validation errors in "formName.$error"

UPDATE on your fiddle:

You didnt set any watcher on that validity variable

Repaired working fiddle is here: Working example

_

Or, you can use "ng-change='inputChanged(event)' " on that input and check validity there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM