Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs ng-minlength validation is not working, form still being submitted

I am trying to submit the form on only successful validation. validation is working for required but not working for ng-minlength

form input is invalid but form is still being submitted.

<form name="myForm" ng-submit="count = count + 1" ng-init="count=0" ng-app>
 <div class="control-group" ng-class="{error: myForm.mobile.$invalid}">
        <label class="control-label" for="mobile">Mobile</label>
        <div class="controls">
            <input type="text" name="mobile" placeholder="07XXXXXXXXX" ng-model="mobile" ng-minlength="11"  required />
            <span ng-show="myForm.mobile.$error.required" class="help-inline">Required</span>
            <span ng-show="myForm.mobile.$error.minlength" class="help-inline">Mobile number should be minimum 11 character starting from 07</span>           
        </div>
    </div>

     <div class="control-group">
        <div class="controls">                       
            <input  class="btn" type="submit" value ="submit" />
        </div>

         count: {{count}}<br />

<tt>myForm.$invalid = {{myForm.$invalid}}</tt><br/>
    </div>
</form>

http://jsfiddle.net/pMMke/9/

what am I doing wrong.

I don't want to use submit button disable method.

like image 720
Developer Avatar asked Aug 29 '13 16:08

Developer


4 Answers

This is what you are doing wrong: you are mixing two concepts, Angular validators and HTML5 validators.

The required HTML5 validators, for instance, states that:

When present, it specifies that an input field must be filled out before submitting the form.

So, if you try to submit a form that has an input with this attribute, it will show a message explaining this to the user, and it will prevent the form from being sent. This is the behavior you want. Why isn't working for ng-minlength? Because ng-minlength is an Angular validator (you can tell because it begins with ng-), and it doesn't add any special behavior to the form. It simply set the input where it is located to invalid (and hence, the form), and let you decide what to do with it.

You have an option: you can use the pattern HTML5 validator, to specify the field requires at least 11 characters. It would like this:

<input type="text" pattern=".{11,}">

So when you submit a form containing this input, it will no be sent if the user has enter less than 11 characters.

But since we are it, and you are already using the pattern validator, you could use the regular expression in its full potential, and define something like:

<input type="text" pattern="07[0-9]{9}" />

Which will only admit values of 11 characters, that start by "07" and that contains only digits. I have modified your fiddle to show you how it would work: http://jsfiddle.net/helara/w35SQ/

like image 181
T.M. Avatar answered Dec 23 '22 20:12

T.M.


I mistakenly used ngMaxlength="12" ngMinlength="6" instead of ng-minlength="6" ng-maxlength="12", it's working fine now.

like image 30
Aqib Mumtaz Avatar answered Dec 23 '22 18:12

Aqib Mumtaz


Both ng-minlength & mg-maxlength works in AngularJS. I've tested this in AngularJS version 1.3.
Make sure to use novalidate with <form> to disable browser's native validation.

like image 42
chaitu301 Avatar answered Dec 23 '22 20:12

chaitu301


This should work:

To enter mobile number

ng-show="myForm.mobile.$touched && myForm.mobile.$error.required"

For minimum length

ng-show="myForm.mobile.$touched && myForm.mobile.$error.minlength"

For maximum length

ng-show="myForm.mobile.$touched && myForm.mobile.$error.maxlength" 
like image 43
GirishBabuC Avatar answered Dec 23 '22 18:12

GirishBabuC