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
a is first two digits
b is next 4 digits
Thanks,
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
a is first two digits
b is next 4 digits
Thanks,
Share Improve this question asked Sep 22, 2016 at 17:26 SnopzerSnopzer 1,70219 silver badges31 bronze badges 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
. – Heretic Monkey Commented Sep 22, 2016 at 17:29 - Do you want to do this during binding (automagically)? Or somewhere in a controller? – Nix Commented Sep 22, 2016 at 17:29
- Yes, the value Is user input – Snopzer 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. – Alberto Rivera Commented Sep 22, 2016 at 17:34
- Thanks @MikeMcCaughan – Snopzer Commented Sep 22, 2016 at 17:35
2 Answers
Reset to default 4You can use plain javascript.
var a = $scope.cardExp.substring(0, 2);
var b = $scope.cardExp.substring(2);
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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745318027a4622309.html
评论列表(0条)