4

I am trying to do a simple form validation using angular js. But it doesnt seems to work and I am not getting what I am missing.

HTML

<form role="form" class="ng-pristine ng-valid" name="registerForm">
                        <fieldset>
                            <div class="form-group">
                                <input class="form-control" placeholder="First Name" name="firstname" type="text"
                                        autofocus="" required ng-class="{'has-error': registerForm.firstname.$invalid}">
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Last Name" name="lastname" type="text" value="">
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="E-Mail" name="email" type="email" value="" ng-class="{'has-error': registerForm.email.$dirty}">
                                <div ng-show="registerForm.email.$dirty">Email Invalid</div>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Password" name="password" type="password" value="" autocomplete="off">
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Organization Name" name="organizationName" type="text" value="">
                            </div>
                            <div class="form-group">
                                <select data-ng-options="item.Display for item in OrganizationTypes track by item.Value" data-ng-model="SelectOrganizationType" class="form-control">
                                    <option value="">-Select Organization Type-</option>
                                </select>
                            </div>
                           
                            <input type="button" value="Register" class="btn btn-lg btn-success btn-block" data-ng-click="Submit()" name="btnSubmit"/>
                           
                        </fieldset>
                    </form>

Now the issue is, I am expecting that if I start entering invalid email then it will show me error message "Email Invalid". But that doesn't happen.

Also when i put a debug before submitting blank form, it says "registerForm.$valid = true"

Any guess what am I missing. Any module that i need to include ?

Here is Plunker

Daniel Cottone
  • 4,257
  • 24
  • 39
Sumit
  • 2,932
  • 6
  • 32
  • 54
  • Please check :http://stackoverflow.com/a/31118444/1638718 – ooozguuur Jul 20 '15 at 14:45
  • possible duplicate of [Adding validation to text field from bootstrap or angularjs](http://stackoverflow.com/questions/31115604/adding-validation-to-text-field-from-bootstrap-or-angularjs) – ooozguuur Jul 20 '15 at 14:45

3 Answers3

4

First, you need registerForm.email.$error.email to trigger the 'invalid email' alert.

Second, I guess you have to bind these input fields with ng-models, but I am not sure if this is necessary.

One more thing, add 'novalidate' to the form element.

 <div class="form-group">
      <input class="form-control" placeholder="E-Mail" name="email" type="email" value="" ng-model="" >
      <div ng-show="registerForm.email.$error.email">Email Invalid</div>
 </div>
NeoWang
  • 17,361
  • 24
  • 78
  • 126
2

Well, I am not sure but I think you also have to use $error with $dirty like this:

<div ng-show="registerForm.email.$dirty && registerForm.email.$error.email">Email Invalid</div>

Also, add novalidate to disable default browser validation like this:

<form role="form" class="ng-pristine ng-valid" name="registerForm" novalidate>
Tarun Dugar
  • 8,921
  • 8
  • 42
  • 79
2

You are missing a bunch of stuff. First of all, you need to have your fields associated with a model for them to be validated. Second, your syntax for accessing errors is incorrect.

Here is a working plunkr for what you are trying to do.

This is the relevant code:

<body ng-controller="MainCtrl">
  <form role="form" name="registerForm" novalidate>
    <fieldset>
      <div class="form-group">
        <input class="form-control" placeholder="First Name" name="firstname" ng-model="firstname" type="text" autofocus="" ng-class="{'has-error': registerForm.firstname.$error.required}" required />
      </div>
      <div class="form-group">
        <input class="form-control" placeholder="Last Name" name="lastname" ng-model="lastname" type="text" value="">
      </div>
      <div class="form-group">
        <input class="form-control" placeholder="E-Mail" name="email" ng-model="email" type="email" value="" ng-class="{'has-error': registerForm.email.$error.pattern}" ng-pattern="/^\S+@\S+\.\S+$/i" required />
      </div>
      <div class="form-group">
        <input class="form-control" placeholder="Password" name="password" ng-model="password" type="password" value="" autocomplete="off">
      </div>
      <div class="form-group">
        <input class="form-control" placeholder="Organization Name" name="organizationName" ng-model="organizationName" type="text" value="">
      </div>
      <div class="form-group">
        <select data-ng-options="item.Display for item in OrganizationTypes track by item.Value" data-ng-model="SelectOrganizationType" class="form-control">
          <option value="">-Select Organization Type-</option>
        </select>
      </div>

      <input type="button" value="Register" class="btn btn-lg btn-success btn-block" data-ng-click="Submit()" name="btnSubmit" />

    </fieldset>
  </form>

  <div ng-show="registerForm.firstname.$error.required">You must enter a first name.</div>
  <div ng-show="registerForm.email.$error.pattern || registerForm.email.$error.required">Email Invalid</div>

</body>
Daniel Cottone
  • 4,257
  • 24
  • 39