Rajesh Hatwar
Rajesh Hatwar

Reputation: 1933

increment given input value by one using jquery

in my project user need to type unique number for every copy of record, this unique number format will be predefined (we have separate option to set the format eg: if format is ' CCNNN ' means 2 char and 3 numbers i.e it might look like ' AA001 ' and it go's on till ' AA999 ', this type of format will set for every section of records, even format may look like this ' CCCCNNNNNNNN...N ' it depends.).

This unique number possibly contain 4 prefixed character and suffix will be numbers, possibly it may weary from 3 digit to ' N ' numbers (eg: ' TSS0000000001234 ')

hear what i need to do is, after inputting first record i will fetch that from database and increment that value by one and i will keep that in entry field (that should not hurt for predefined format) i.e before entering next record, something like reducing user effort (suppose user entered like ' TS001 ' after submitting record entry field will ready with unique number for next entry ' TS002 ' like wise for every format of unique number i keep increment.)

I tried in this way (i think i am using totally wrong logic) http://jsfiddle.net/hatwar/5bk6E/...

var givenInput="TS0001";//input different format eg: MAM7612 or HSD8723

// hear prefix possibly character and sufix probably integer


var separation = givenInput.substring(2); // 


var gettingLastNo  = separation[separation.length-1];


var incrementedValue = gettingLastNo.replace(/(\d+)/, function(){return arguments[1]*1+1});

var conditionCheck = '1';

if(incrementedValue.length > conditionCheck){

   document.write((givenInput.substring(0, givenInput.length-2))+incrementedValue);     

}else{

   document.write((givenInput.substring(0, givenInput.length-1))+incrementedValue);
}

i have no problem if user enter unique number like ' TS1, TS2 , TS3... ' i can easily increment but it probably contain ' zeros ' that's why i tried like above but it fail when user enter like ' TS199 ' even i can keep another condition and increment it but it's not a better logic

any help will be appropriated. Thanks.

Upvotes: 0

Views: 197

Answers (2)

Rakesh
Rakesh

Reputation: 746

Please try with this simple code. This will give you a result as you explain.

var givenInput = "TSBC020145";
var myNumber = givenInput.match(/\d+/g)[0];
var myString = givenInput.match(/\D+/g)[0];
var myIncrement = ++givenInput.match(/\d+/g)[0];

var myFinalNumber = myString + myNumber.substr(0, (myNumber.length -      myIncrement.toString().length))+myIncrement;

'myFinalNumber' has the result value as you want. Result will be 'TSBC020146'.

Upvotes: 1

Joe
Joe

Reputation: 28366

Since you have already split the prefix from the number, you could use parseInt to convert from string to integer:

var incrementedValue = parseInt(gettingLastNo) + 1;

If you don't know the pattern ahead of time, you could use a regular expression to split the input

var matched = givenInput.match(/^([^\d]+)(\d+)$/);

if (matched === null) {
    //process invalid input
} else {
  if (matched.length != 3) {
    //also invalid input
  } else {
     var prefix = matched[1];
     var inputNoLen = matched[2].length;
     var lastNo = parseInt(matched[2]);
     var nextNo = lastNo + 1;
     var ouputNo = "" + nextNo;
     if (outputNo.length < inputNoLen) { outputNo = Array(inputNoLen - outputNo.length + 1).join("0") + outputNo; }

  }
}

Upvotes: 0

Related Questions