4

This is my code and I want to set focus on first name textbox on form submit if first name textbox has error like $error.required,$error.pattern,$error.minlength or $error.maxlength.

<form class="form-horizontal" name="clientForm" id="clientForm" novalidate ng-submit="clientForm.$valid" ng-class="{ loading:form.submitting }">
<div class="form-group">
    <label class="col-lg-2 control-label">First Name</label>
    <div class="col-lg-8">
        <input type="text" ng-model="client.firstName" class="form-control" required autofocus name="firstName" autocomplete="off" ng-maxlength="100" ng-minlength=3 ng-pattern="/^[a-zA-Z]*$/" />
        <!--<span ng-show="clientForm.firstName.$dirty && clientForm.firstName.$invalid" class="error">First Name is required.</span>-->
        <div class="errors" ng-show="clientForm.$submitted || clientForm.firstName.$touched">
            <div class="error" ng-show="clientForm.firstName.$error.required">
                First Name is required.
            </div>
            <div class="error" ng-show="clientForm.firstName.$error.pattern">
                Enter valid name.
            </div>
            <div class="error" ng-show="clientForm.firstName.$error.minlength">
                First Name is required to be at least 3 characters
            </div>
            <div class="error" ng-show="clientForm.firstName.$error.maxlength">
                First Name cannot be longer than 100 characters
            </div>
        </div>
    </div>
</div>
<button type="submit" class="btn btn-primary">Save</button></form>

3 Answers 3

7

This question is a duplicate of: Set focus on first invalid input in AngularJs form

You can use a directive on the form:

<form accessible-form>
    ...    
</form>

app.directive('accessibleForm', function () {
    return {
        restrict: 'A',
        link: function (scope, elem) {

            // set up event handler on the form element
            elem.on('submit', function () {

                // find the first invalid element
                var firstInvalid = elem[0].querySelector('.ng-invalid');

                // if we find one, set focus
                if (firstInvalid) {
                    firstInvalid.focus();
                }
            });
        }
    };
});

Here is a working Plunker: http://plnkr.co/edit/wBFY9ZZRzLuDUi2SyErC?p=preview

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

Comments

0

You'll have to handle this using directive. For example:

It will iterate through the element(s) and check if the focusNow attribute is true or not. Make sure that the error handler code sets the expression true/false.

.directive('focusNow', function ($timeout) {
    return {
        link: function (scope, element, attrs) {
            scope.$watch(attrs.focusNow, function (value) {
                if (value === true) {
                    for (var i = 0; i < element.length; i++) {
                        var ele = angular.element(element[i].parentNode);
                        if (!ele.hasClass('ng-hide')) { //Skip those elements which are hidden.
                            element[i].focus();
                        }
                    }
                }
            });
        }
    };
});

and in the HTML, you'll have:

<input type="text" focus-now="expression" />

where expression will be a normal expression which will evaluate to true in case of error.

Comments

0

You can try this: i.e add ng-focus="clientForm.$error" in first name input

<input type="text" ng-focus="clientForm.$invalid" ng-model="client.firstName" class="form-control" required autofocus name="firstName" autocomplete="off" ng-maxlength="100" ng-minlength=3 ng-pattern="/^[a-zA-Z]*$/" />

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.