0

Here's my current html code:

<div class="form-group" ng-class="{ 'has-error' : form.inputName.$invalid && !form.inputName.$pristine }">
    <label for="inputName" class="col-sm-4 control-label"><font color='RED'>* </font>Name</label>
    <div class="col-sm-8">
        <input type="text" class="form-control" ng-model="name" name="inputName" id="inputName" placeholder="Enter in a name." required />
        <p ng-show="form.form.$invalid && !form.form.$pristine" class="help-block">Name is required.</p>
    </div>
</div>

Now this works well for fields that are mandatory for any case.

However, let's say the user could pick a tab like Hair Style and had to describe the color field and the length field. If these fields are blank, then display a small message (like ng-show). So based on whatever tab the user selects, if the proper information for that tab is not inputted, the user should see an error. How would I use angular to display that?

Eg. Controller code...

$scope.someButtonPressed = function() {
    if ($scope.color == "") {
        // display ng-show for color
        // input field should have a red border to it
    }
}

Here is where I got my html error validation code from: http://codepen.io/sevilayha/pen/xFcdI

1 Answer 1

3

You should refer https://scotch.io/tutorials/angularjs-form-validation which has detailed explanation and an example.

Also this doc is must a read!!

If you want to make border color for a compulsory field then refer this. You could use ng-invalid class for ng-required fields

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

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.