ThomasD
ThomasD

Reputation: 2494

validating input field using required and angularjs by clicking button outside of form

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

Upvotes: 0

Views: 3417

Answers (2)

Mukesh Soni
Mukesh Soni

Reputation: 6668

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;
    }
};

Upvotes: 1

eddiec
eddiec

Reputation: 7646

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.

Upvotes: 0

Related Questions