0

Adding "required" to an input field fires validation when the clicked button is within the form. But how do I trigger validation when clicking a button outside the form ?

I have created a plunker here demonstrating the issue: http://plnkr.co/edit/rxWHRxi3w8aJhkpO427c?p=preview

If you do not enter anything in the input field and click the button within form, then the validation is executed. But if you click the button outside the form, nothing happens.

How do I trigger validation when clicking a button outside the form ?

thanks

Thomas

2 Answers 2

1

Give the form a name, which will then be added as property to $scope object. Say myForm.

<form novalidate name="myForm">

Now in your submit function, you can check for form validity -

$scope.submit=function() {
    if(!$scope.myForm.$valid) {
        $scope.myForm.mytext.$dirty = true;
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. It now fires the validation. How do I make it show the validation message ? In the original plunk, it shows a validation error when clicking the button inside the form, but in the updated plunk (based on your answer), it doesn't show the validation error.
0

The form is invalid, you can see the class ng-invalid on the form element if you inspect it. Native HTML5 validation will only trigger if you click on the submit button within the form.

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.