1

How to slice the input text Field Value using Angular JS

HTML FORM

<form>
<input type="text" ng-model="cardExp">
</form>

JS

console.log($scope.cardExp) //output : 122016

I want to make text field value ("122016") into variables a and b and their vales are

  1. a is first two digits

  2. b is next 4 digits

Thanks,

7
  • 1
    AngularJS is JavaScript, so you would do it the same in AngularJS as you would in JavaScript. You'll want to look into substring. Commented Sep 22, 2016 at 17:29
  • Do you want to do this during binding (automagically)? Or somewhere in a controller? Commented Sep 22, 2016 at 17:29
  • Yes, the value Is user input Commented Sep 22, 2016 at 17:32
  • 1
    If this is regarding the expiration date of something, from a UX point of view, I would suggest separating year and month into two different inputs. Commented Sep 22, 2016 at 17:34
  • Thanks @MikeMcCaughan Commented Sep 22, 2016 at 17:35

2 Answers 2

4

You can use plain javascript.

var a = $scope.cardExp.substring(0, 2);
var b = $scope.cardExp.substring(2);
Sign up to request clarification or add additional context in comments.

Comments

1
 var app = angular.module('store', []);
  app.controller('StoreController', function($scope) {
    $scope.str = '122016';
    $scope.splited = $scope.str.match(/.{1,4}/g);
    $scope.a = $scope.splited[0];
    $scope.b = $scope.splited[1];
  });

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.