Invalidate form if disabled select option is chosen

Issue

Is there a way to invalidate a form if value="0" in my select tag is chosen? The reason I did this is to have a default select option show up before a user see’s anything.

But even if a user doesn’t choose anything, the form is valid.

<form name="add_task_frm" ng-submit="!add_task_frm.$valid || functiontoUse()" novalidate>

    <input type="text" ng-model="name" required />

    <select ng-model="action" required>
        <option value="0" selected disabled>Choose an Action</option>
        <option value="description">See Description</option>
        <option value="view">View</option>
    </select>

    <button type="submit">Submit</button>

</form>

Solution

I think you could just remove the value from the first option this should make it invalid if the user does not select anything. However I would recommend changing this implementation to use ng-options.

https://docs.angularjs.org/api/ng/directive/ngOptions

Answered By – Joe

Answer Checked By – Senaida (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.