Reputation:
I am trying this to make the last digit increment, it has 3 numbers on it for example, ABC123-DE45-000 How will I make the last 0 increment to 1, it will look like ABC123-DE45-001,ABC123-DE45-002,ABC123-DE45-003 and so on and so fort, I tried doing this code, turns out that the 000 will increment into 1 not 001 and I get it because (\d+) finds a string which has a number then converts it into Integer, but is it possible for my problem to be like what I am asking for? Thanks for the answer.
Here is my script
function addInputs(pass) {
var n = pass.value && parseInt(pass.value, 10);
if (isNaN(n)) {
return;
}
}
$('button').click(function() {
var value = $('#sel_control_num').val();
for (var i = 1; i <= 5; i++) {
value = value.replace(/(\d+)$/, function(match, n) {
console.log(value);
return ++n;
});
$('#parent')[0].innerHTML += '<br>' + value;
}
})
Here is my HTML
<div>
<label> Control No </label>
<input name="get_control_num" style="text-transform:uppercase"
class="form-control" id="sel_control_num" readonly>
</div>
<div class="form-group">
<label> Quantity </label>
<input class="form-control" name="quantity" type="number"
onchange="addInputs(this)" />
<br>
<button type="button" class="btn btn-primary"> Add Control Number </button>
</div>
<div class="form-group" id="parent"></div>
Upvotes: 1
Views: 97
Reputation: 3520
With Lookbehind
, you can match -
before the number and do:
value = value.replace(/(?<=-)\d{3}/g, function(match, n) {
const nextValue = ++match;
return ('000'+nextValue).slice(-3);
});
Upvotes: 2