Chris
Chris

Reputation: 27384

How to remove or disable input from form validation

I have an input that is optionally disabled. When disabled it still seems to take part in form validation. How can I remove this input from form validation?

<form ng-app name="myForm">
    <label>Name</label>
    <input type="text" name="name" ng-model="form.name" ng-disabled="show" required>
    <input type="submit" ng-disabled="myForm.name.$invalid">
    <input type="submit" ng-click="show = !show" value="Toggle">
</form>

Notice on the example that when you hit toggle the input is disabled but the Submit button does not become enabled.

Upvotes: 2

Views: 611

Answers (2)

Varun Chakervarti
Varun Chakervarti

Reputation: 1042

You can do it with pure JavaScript. By searching for the attribute you created.

function validateForm() {
    var inputElements = document.forms["myForm"].getElementsByTagName("input");
    for(inpt_ele=0; inpt_ele<inputElements.length; inpt_ele++) {
        if(inputElements[inpt_ele].getAttribute("disabled") == "disabled") {
            //skip
        }
        else {
            //validate
        }
    }
}
validateForm();

Upvotes: 0

Lander Van Breda
Lander Van Breda

Reputation: 873

You can use ng-required in this case :

<form ng-app name="myForm">
    <label>Name</label>
    <input type="text" name="name" ng-model="form.name" ng-disabled="show" ng-required="!show">
    <input type="submit" ng-disabled="myForm.name.$invalid">
    <input type="submit" ng-click="show = !show" value="Toggle">
</form>

Upvotes: 1

Related Questions