I want to insert hyphen to validate a ID number. For example, my card number format would be something like 123 - 2342 - 1232321 - 1 (over all only 15 numbers) user should not be allowed to enter more than 15 digits. I need to add hyphen once the above set of digits is entered by the user with a space before and after each digit. First hyphen should be added after 3 digits , secone hyphen after entering 4 digits, third hyphen to be added after entering 7 digits. How can I validate this?
<div>
<input type="text" id="tbNum" ng-keypress="addHyphen(this)"
placeholder="Type some values here" />
</div>
$scope.addHyphen = function(element) {
let ele = document.getElementById(element.id); // ele doesnt have id value
ele = ele.value.split('-').join('');
let finalVal = ele.match(/.{1,3}/g).join('-');
document.getElementById(element.id).value = finalVal;
};
I tried to implement the above way , but element.id doesnt capture the id value of the input field.
this, can't you pass$eventand read in thesrcElementproperty?