angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Validation for ngMaxlength and ngMinlength is not working

Open camilolopes opened this issue 13 years ago • 19 comments

<input name="password" ng-model="password" type="password" ngMaxlength="12" ngMinlength="6"     ng-required="true" />

I have created this input but ngMaxlength and ngMinlength is not working.For example below a typed only one character for password and it was considered valid for my form.

camilolopes avatar Aug 20 '13 03:08 camilolopes

Your example is missing.

jafp avatar Aug 23 '13 09:08 jafp

hi @jafp is missing? May you more specific, please?

camilolopes avatar Aug 23 '13 11:08 camilolopes

Hi Camilo, It sounds like you are referring to an example, but I cannot see that example anywhere?

jafp avatar Aug 23 '13 12:08 jafp

@jafp here an example input name="password" ng-model="user.password" type="password" ngMaxlength="12" ngMinlength="6" ng-required="true" size="20" It is not working

camilolopes avatar Aug 23 '13 21:08 camilolopes

http://stackoverflow.com/questions/18516001/angularjs-ng-minlength-validation-is-not-working-form-still-being-submitted

see example here http://jsfiddle.net/pMMke/9/

asifjaveduk avatar Aug 29 '13 16:08 asifjaveduk

I noticed this as well.

After looking at angular.js, I noticed the following lines..

13894: if (attr.ngMinlength) { 13895: var minlength = int(attr.ngMinlength);

But when I look at the html for my form it's turning my ngMinlength attribute to "ngminlength" which makes the attribute come up as undefined and no validation is attached. I can't seem to find the area of the code that's putting the attribute to lowercase which would fix the issue. However, swapping ngMinlength with ngminlength seems to work.

danieljwest avatar Sep 20 '13 12:09 danieljwest

I think what you want is ng-minlength (note the dash) on your form element instead.

lord2800 avatar Sep 20 '13 17:09 lord2800

@camilolopes you need to write your attributes like ng-maxlength="12" ng-minlength="6" instead of ngMaxlength="12" ngMinlength="6".

Please attach a life reproduce scenario using plnkr.co while opening issues in the future.

pkozlowski-opensource avatar Sep 22 '13 05:09 pkozlowski-opensource

@pkozlowski-opensource alright. So, the documentation was not right. Thanks. It worked very well.

camilolopes avatar Sep 24 '13 04:09 camilolopes

Hi, sorry but the example still does not work for me, the count is increasing even when the length is less than required

emardini avatar Sep 26 '13 03:09 emardini

@emardini if you want block the user does not type when the max is achieved you should not use ng-maxlength. In this case use maxlength only.

camilolopes avatar Sep 26 '13 03:09 camilolopes

Can't get this working either. Just used the todo example from the Angular website and added the attribute to my input element. I don't get validation errors if the todo name is to short and even worse the todo gets added to the list without name. http://jsfiddle.net/P8D2j/1/

timjacobi avatar Apr 18 '14 22:04 timjacobi

You need to test formName.$valid, formName.$error, formName.inputName.$valid, $formName.inputName.$error and let your application respond appropriately to those errors... http://jsfiddle.net/Td98M/1/

The docs describe this well: https://docs.angularjs.org/api/ng/directive/input

shahata avatar Apr 18 '14 22:04 shahata

Thanks for the hint. I'm wondering why ng-required works without these checks and doesn't let me submit the form when I click "add" while the input is empty. Would be great to have a consistent behaviour for all attributes.

timjacobi avatar Apr 18 '14 23:04 timjacobi

This is because <input required> is supported natively in your browser via HTML5. Sadly, HTML5 doesn't have minlength support. (it does have pattern matching, but that's not exactly the same)

shahata avatar Apr 18 '14 23:04 shahata

I was using ng-required though. Btw I was able to add invalid items to the list in your fiddle by hitting return so that workaround with hiding the button doesn't fix it completely.

timjacobi avatar Apr 18 '14 23:04 timjacobi

The ng-required directive adds the required attribute to the element. I wasn't attempting to solve the problem, just show you how to approach it... To prevent submitting all together just test $scope.myForm.$valid in your ngSubmit method.

shahata avatar Apr 18 '14 23:04 shahata

Alright, thanks for the effort, lesson learned I guess. I presume the HTML5 behaviour is confusing to many people. Will go with a FormController then. Cheers.

timjacobi avatar Apr 18 '14 23:04 timjacobi

It might help you to validate the ng-minlength, ng-maxlength and ng-required in AngularJs http://www.code-sample.com/2015/06/ng-minlength-ng-maxlength-and-ng.html

anilsingh581 avatar Jun 20 '15 12:06 anilsingh581