Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do conditional maxlength in angularjs

does anyone know how to do conditional maxlength in angularjs, I have found conditional required but not maxlength etc....

for example if maxlength given ...it should validate but if maxlength is 0 or false, it should not....

$scope.validation = {a: {required:true,minlength:5, maxlength:10}, 
                     b: {required:false,minlength:0, maxlength:false}}

Html gets created using ng-repeat loop....n now problem is maxlength gets fired everytime...whereas i have disabled it for second input field

<input type="text" name="myName" ng-model="name" ng-minlength='validation.a.minlength' ng-maxlength="validation.a.maxlength" ng-required='validation.a.required'  />


<input type="text" name="myName1" ng-model="name1" ng-minlength='validation.b.minlength' ng-maxlength="validation.b.maxlength" ng-required='validation.b.required' />
like image 233
Luckyy Avatar asked Oct 31 '22 19:10

Luckyy


1 Answers

These parameters seems to work, if you use empty strings as "false" via angular expressions.

<input type="text" name="myName" ng-model="name" ng-minlength='{{validation.a.minlength}}' ng-maxlength="{{validation.a.maxlength}}" ng-required='validation.a.required'  />
<input type="text" name="myName1" ng-model="name1" ng-minlength='{{validation.b.minlength}}' ng-maxlength="{{validation.b.maxlength}}" ng-required='validation.b.required' />

  $scope.validation = {
    a: {required:true, minlength:5, maxlength:10}, 
    b: {required:false, minlength:'', maxlength:''}
  }

plunker

plunker for 1.3.2

Now its possible to use 0 and Number.MAX_SAFE_INTEGER to ignore this rules. Or maybe just 999 will be enough :)

 b: {required:false, minlength:0, maxlength:Number.MAX_SAFE_INTEGER}
like image 118
Qustion Avatar answered Nov 12 '22 14:11

Qustion