Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello all ..Can any one notify me about the point i am missing

XML
<ng-form name="myForm" novalidate ng-app="myApp" ng-controller="appCtrl">

             <div class="form-group"  ng-class="{'has-error': myForm.name.$invalid && !myForm.name.$pristine}">
                <label>Name</label>
                <input type="text" required ng-model="name" name="name"  />
            </div>

            <div class="form-group"  ng-class="{'has-error': myForm.age.$invalid}">
                <label>Age</label>
                <input type="text" required ng-model="age" name="age"  />
            </div>

            <input type="button" ng-click="click()" value="clcik"  class="btn btn-primary" />

         </ng-form>



this code is not working properly .. what i want is when the page loads the 'has-error' should be applied on name and age inputs and they should be bordered with color ( highlight the fields). but in my case this is not happening. I dont know what i am missing.

PHP
angular.module('myApp', []).
controller('appCtrl', function ($scope) {
    $scope.name = "";
    $scope.age = "";


    $scope.click = function () {
        if ($scope.myForm.$valid) {
            alert('our form is amazing');
        }
    }

});


Also when i write my own 'has-error' css class style. the whole div gets higlighted.
I have included bootstrap.min and angular js.. can any one help
Posted
Comments
Simon_Whale 6-Nov-14 4:56am    
So just to make sure that I am correct you want to validate the form on load before the user begins entering information?
SaadZulfiqar 6-Nov-14 10:43am    
Yes Simon.

Dear SaadZulfiqar use ng-class in name and password input tag instead of form-group

Demo Fiddle:http://jsfiddle.net/x058sjb1/3/[^]

HTML
<div class="form-group">
                <label>Name</label>
                <input type="text" required ng-model="name" name="name" ng-class="{'has-error': myForm.name.$invalid}" />
            </div>
 
            <div class="form-group">
                <label>Age</label>
                <input type="text" required ng-model="age" name="age" ng-class="{'has-error': myForm.age.$invalid}" />
            </div>
 
Share this answer
 
v4
Comments
SaadZulfiqar 6-Nov-14 10:42am    
Thanks. It worked, by the way in order to have bootsratp 'has-error' style we have to add form-control class in the respective input tag.
Quote:
Also if i add !important in the css to override it,than it worked also :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900