jcm
jcm

Reputation: 5659

AngularJS validation on submit only

I want to implement some simple form validation in my AngularJS app, but I don't want it to show any validation errors until after the user has clicked the form submit button. I don't want it to validate as I type or even on exiting the field.

Is there a way to do this? I'll need to write at least one custom validator directive so it will need to work with that.

I am finding form validation in AngularJS to be very difficult so far, it's hard to get it to work exactly as you want.

Upvotes: 1

Views: 679

Answers (1)

BKM
BKM

Reputation: 7079

You could do something like this. Here's an 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>

Upvotes: 1

Related Questions