Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an input field to ng-invalid in angularJS

I would like to check an input field for credit card numbers.

The field should remain invalid until it has a minimum length of 13. As the user should be able to fill in space into the field, I remove those spaces within a javascript function. In this function I would like to check the credit card number (without spaces) and set it to ng-invalid as long as the minimum length is lesser than 13 and the maximum length is greater than 16.

It should be something like this:

$scope.ccHandler = function() {
   if ($scope.ccNumber == '') {
      document.getElementById("ccProvider").disabled = false;
   }
   $scope.ccNumber = inputCC.value.split(' ').join(''); //entfernt die Leerzeichen aus der Kreditkartennummer vor der übergabe an den Server
   console.log("das ist meine CC: " + $scope.ccNumber);
   isValidCreditCardNumber($scope.ccNumber);
   getCreditCardProvider($scope.ccNumber);
   document.getElementById("ccProvider").disabled = true;
   if ($scope.ccNumber.length < creditCardNumberMinLength || $scope.ccNumber.length > creditCardNumberMaxLength) {
      //$scope.ccNumber.ng-invalid = true;
      console.log("ccNumber ist noch ungültig!");
      //document.getElementById("inputCC").$setValidity("ccNumber", false);
   }
}

This would be the part of the XHTML:

<div class="form-group" ng-switch-when="CreditCard">
   <label class="col-xs-3 col-sm-3 control-label">Kreditkartennummer</label> 
   <div class="col-xs-5 col-sm-5 col-md-5">
      <input name="ccNumber" class="form-control" type="text" id="inputCC" ng-change="ccHandler();updateCount()" ng-model="ccNumber" ng-required="true"/>
   </div>
</div>

How can I achieve this?

like image 232
Pille Avatar asked Apr 10 '14 08:04

Pille


People also ask

How do you make an input field invalid?

setCustomValidity("Invalid field."); will make the field invalid. field. setCustomValidity(""); will make the field valid unless it fails an HTML5 constraint. Also, if you want the browser's validity message to show up on input or blur you can use field.

What is Ng invalid in angular?

ng-invalid The field content is not valid.

What is $setValidity in AngularJS?

The $setValidity() function is a built-in AngularJS function that is used to assign a key/value combination that can be used to check the validity of a specific model value. The key in this case is “unique” and the value is either true or false.

What is Ng dirty?

ng-dirty: The ng-dirty class tells that the form has been made dirty (modified ) by the user. It returns true if the user has modified the form. Return type: Return Boolean True if the form/input field is modified by the user else it returns False.


2 Answers

You can do that with $setValidity. You need to set a name attribute on the <form> for this to work.

<form name="myForm">
   <input name="ccNumber" ng-model="ccNumber">
   <span ng-show="myForm.ccNumber.$error.minLength">
      A cc number should be minimum 10 chars
   </span>
</form>

Then you can manually set the validity with

$scope.myForm.ccNumber.$setValidity("minLength",$scope.ccNumber.length>=10);

Here is a working plnkr: http://plnkr.co/edit/7J326LR194SaoE2TmHuU?p=preview

like image 63
Pieter Herroelen Avatar answered Oct 11 '22 06:10

Pieter Herroelen


The following code worked for me:

$scope.myForm.ccNumber.$setValidity("ccNumber", false);
like image 31
Pille Avatar answered Oct 11 '22 04:10

Pille