1

I want to display validation message after user clicks on the submit form. Eg Text box is required and user directly clicks on the submit button then the validation field should be displayed.

Here when the user clicks on the form nothing happens.

<body ng-controller="AppController as ctrl">

<form ng-submit="ctrl.submit()" name="myForm" novalidate>
    <input type="text" ng-model="ctrl.user.username" name="uname" class="username" placeholder="Enter your name" required ng-minlength="3" />
    <span ng-show="myForm.$dirty && myForm.uname.$error.required">This is a required field</span>

    <input type="text" ng-model="ctrl.user.address" placeholder="Enter your Address" /><br /><br />

    <input type="email" ng-model="ctrl.user.email" name="email" class="email" placeholder="Enter your Email" required />
    <span ng-show="myForm.$dirty && myForm.email.$error.required">This is a required field</span>

    <input type="submit" value="Submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js">
</script>
<script>
    angular.module('myApp', [])
    .controller('AppController', [function () {
        var self = this;
        self.submit = function () {
            console.log('Form is submitted with following user', self.user);
        };
    }]);
</script>

1 Answer 1

2

When angular submit a form it make $submitted property on form level to true, also it adds ng-submitted class on form's class attribute.

You could use $submitted property of form object like myForm.$submitted to identify that form has been submitted or not

ng-show="myForm.$submitted && myForm.uname.$error.required"

Demo Plunker

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks,am am learning angular 1 can you suggest some useful links.

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.