0

I'm trying to disable submitting the form until all fields are filled.

I've tried adding the disable option in the button but it doesn't work.

This is the form:

<form id="contactForm" name="contactForm" novalidate>
    <input type="text" name="contactName" id="contactName" placeholder="Nombre" data-ng-model="cu.contactName" ng-model-options="{ updateOn: 'blur' }" required autofocus>
    <span style="color:red" ng-show="contactForm.contactName.$dirty && contactForm.contactName.$invalid">
        <span ng-show="contactForm.contactName.$error.required">El nombre es requerido.</span>
    </span>

    <button class="hvr-bounce-to-right" type="submit" ng-disabled="contactForm.contactName.$dirty && contactForm.contactName.$invalid" name="submit-form" ng-click="cu.sendMail()">Enviar mensaje &ensp; <span class="icon flaticon-envelope32"></span></button>
</form>

Right now, the button gets disabled when I start typing and then deletes everything in the input, when I try to click, it gets disabled, but what I want is that the button is disabled since the beginning and then when you fill the inputs it gets unlocked or un-disabled(if that's a word).

Please help, thanks!

1
  • 1
    Enabled is the word you were looking for :) Commented Nov 13, 2015 at 19:53

3 Answers 3

1

The angular docs are actually pretty decent for form state handling:

https://docs.angularjs.org/guide/forms

Check the second-to-last example, where they display a message if the email is invalid.

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

Comments

0

Each state of every input should be reflected in the scope/controller.

So assuming you have this :

$scope.myInputs = {};

you should ng-model each input in the view to :

ng-model = "myInputs.email";

Then , using ng-blur/ng-change you should iterate through each property in the $scope.myInputs object to see if values are filled. only then you should control the disability of the submit.

Comments

0

I was able to solve this by adding this $pristine attribute:

User has not interacted with the field yet.

So my button looks now like this:

<button class="hvr-bounce-to-right" type="submit" ng-disabled="contactForm.contactName.$dirty && contactForm.contactName.$invalid || contactForm.contactName.$pristine" name="submit-form" ng-click="cu.sendMail()">Enviar mensaje &ensp; <span class="icon flaticon-envelope32"></span></button>

Now it is disabled from the start and only enables itself once the user interacts and fill the (in this case) contactName input.

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.