Daniel Daniel - 1 year ago 44
AngularJS Question

How to validate forms in Angular JS

i'm a newbie in angular JS. and i was trying the input

, i'm retrieving my name from my controller and i was using
to validate my data but it isn't showing when i make the input field empty.

JS Fiddle : https://jsfiddle.net/U3pVM/24718/

var FormValidations = angular.module('FormValidations',[]);

FormValidations.controller('FormsValidations', function( $scope ){

$scope.formsToBeValidated = {
firstName : 'Daniel'


Answer Source

Unless you add name attribute to form level field, that will not get included in form object when you specified in the name attribute on form, currentlyh it is name="studentForm". Do add name="firstName" to your input field

<input type="text" class="form-control" 
  name="firstName" ng-model="formsToBeValidated.firstName" required/>

Forked Fiddle

For more information you could refer this answer, which has more detailed

More convenient way to solve this issue would be using ng-messages instead of using ng-show/ng-hide, for that you have to include ng-messages module with its ng-messages dependency.