2

I am trying to validate a form and display the validation result just under the text fields on submission of form as below.

But i am not able to validate the input field on submit , but i am able to validate onBlur, i tried both ways, see belo the codes for onSubmit and onBlur respectively.

My codes for this is:

formSumbit.html

<!DOCTYPE html>
<html lang="en" ng-app="testapp">
<head>
    <meta charset="utf-8"/>
    <title>BoilerPlate</title>
    <link rel="stylesheet" type="text/css" href="/reset.css"></link>
    <link rel="stylesheet" type="text/css" href="/lib/bootstrap/css/bootstrap.css"></link>


    <script src="/lib/angular/angular.js" ></script>
    <script src="/lib/angular/angular-route.js" ></script>
    <script src="/jquery.min.js" ></script>
    <script src="/lib/bootstrap/js/bootstrap.js" ></script>

    <script>
    var app=angular.module('testapp', ['ngRoute']);
     app.controller('signupController', ['$scope', function($scope) {
      $scope.submitted = false;
      $scope.signupForm = function() {
        console.log("hi");
        if ($scope.signup_form.$valid) {
          // Submit as normal
        } else {
          $scope.signup_form.submitted = true;
        }
      }
    }]);
    </script>
    <style type="text/css">
    .error{color:red}
    </style>

  </head>
  <body>
    <div>
            <form name="signup_form"  ng-submit="signupForm()" novalidate >
                <div class="form-group">
                    <label for="name">Name</label>
                    <input type="text" name="name"  ng-model="testform.name" required class="form-control" id="name" placeholder="Name"/>
                    <div class="error" ng-show="signup_form.name.$dirty && signup_form.name.$error.required && signup_form.submitted">
                        <small class="error">
                            Your name is required.
                        </small>

                    </div>
                </div>
                <div class="form-group">
                    <label for="email">Email address</label>
                    <input type="email" name="email"  ng-model="testform.email"  class="form-control" id="email" placeholder="Enter email"/>
                     <div class="error" ng-show="signup_form.email.$dirty && signup_form.email.$invalid && signup_form.submitted">
                        <small class="error" ng-show="signup_form.email.$error.email">
                                Your email not valid
                        </small>
                     </div>
               </div>
               <button type="submit" class="btn btn-default">Submit</button>
            </form>
    </div>
  </body>
</html>

But i am able to do the validation in on Blur event. see the code below which is working on onBlur.

<!DOCTYPE html>
<html lang="en" ng-app="testapp">
<head>
    <meta charset="utf-8"/>
    <title>BoilerPlate</title>
    <link rel="stylesheet" type="text/css" href="/reset.css"></link>
    <link rel="stylesheet" type="text/css" href="/lib/bootstrap/css/bootstrap.css"></link>


    <script src="/lib/angular/angular.js" ></script>
    <script src="/lib/angular/angular-route.js" ></script>
    <script src="/jquery.min.js" ></script>
    <script src="/lib/bootstrap/js/bootstrap.js" ></script>

    <script>
    var app=angular.module('testapp', ['ngRoute']);
     app.controller('signupController', ['$scope', function($scope) {

    }]);
    </script>
    <style type="text/css">
    .error{color:red}
    </style>

  </head>
  <body>
    <div>
            <form name="signup_form"   novalidate >
                <div class="form-group">
                    <label for="name">Name</label>
                    <input type="text" name="name"  ng-model="testform.name" required class="form-control" id="name" placeholder="Name"/>
                    <div class="error" ng-show="signup_form.name.$dirty && signup_form.name.$error.required">
                        <small class="error">
                            Your name is required.
                        </small>

                    </div>
                </div>
                <div class="form-group">
                    <label for="email">Email address</label>
                    <input type="email" name="email"  ng-model="testform.email"  class="form-control" id="email" placeholder="Enter email"/>
                     <div class="error" ng-show="signup_form.email.$dirty && signup_form.email.$invalid">
                        <small class="error" ng-show="signup_form.email.$error.email">
                                Your email not valid
                        </small>
                    </div>
               </div>
               <button type="submit" class="btn btn-default">Submit</button>
            </form>
    </div>
  </body>
</html>
2
  • 2
    This question appears to be off-topic because it is about typo. Commented Dec 18, 2013 at 12:25
  • 1
    @Stewie I corrected the Typo, still i am not able to do the validation on submit button click. I edited the question. Commented Dec 18, 2013 at 13:53

2 Answers 2

3

Try this:

Example

<form name="form" ng-app>
 <div class="control-group" ng-class="{true: 'error'}[submitted && form.email.$invalid]">
     <label class="control-label" for="email">Your email address</label>
      <div class="controls">
            <input type="email" name="email" ng-model="email" required />
            <span class="help-inline" ng-show="submitted && form.email.$error.required">Required</span>
            <span class="help-inline" ng-show="submitted && form.email.$error.email">Invalid email</span>
        </div>
    </div>

    <button type="submit" class="btn btn-primary btn-large" ng-click="submitted=true">Submit</button>
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

:Thanks for the help. Still i am getting the error which i have shown in the SS in the question. Is it possible to access the form.email.$error.required in controller?
Finally i found out the issue for the "unexpected identifier". This is beacause i also copied the "Window size: x Viewport size: x" when i downloaded the angular-route.js files from angular site. now after emoving it from my library angular file it is solved.
This was the key for me: ng-click="submitted=true"
0

Try to set the controller on the container div

<div ng-controller="signupController">

Check this example http://codepen.io/sevilayha/pen/xFcdI

Comments

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.