Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-minlength is valid when length is 0

Tags:

angularjs

I'm new to angular and stumbled upon a validation problem in a form. Below is my input field and when I don't enter any value it show's as valid and also 'ng-valid' class is added to it even though I've defined minlength to be 5

<input id="name" type="text" placeholder="Name" ng-model="formdata.name" ng-minlength=5 />

It happens even in angular example - http://docs.angularjs.org/api/ng.directive:input

How can I resolve this?

like image 777
user1184100 Avatar asked Dec 22 '13 14:12

user1184100


1 Answers

You need to set the ngRequired parameter as well. Without it the minlength parameter means that if the user has entered a value it must be at least 5 characters long, but it's ok to leave the field empty.

<input 
    id="name" 
    type="text" 
    placeholder="Name" 
    ng-model="formdata.name" 
    ng-minlength="5" 
    ng-required="true" />
like image 105
JJJ Avatar answered Sep 17 '22 14:09

JJJ