Validate number to 4 digits

stevehardstaff
Giga Contributor

Hi all

I am currently able to strip a store number from an inbound email. Trouble I have is that they could be, 1 digit, 2 digit, 3 digits or 4. The email I receive leaves out the leading zeros and I hold them as 4 digits (with the leading zeros). E.g, Inbound email has "1" whereas I hold "0001". My code is:

var store=email.body_text.split('\n');

for(i=0; i<store.length; i++){

if(store[i].indexOf('Store Epos:')>-1) {

var storeIs = (store[i].trim().split(':')[1].trim());

  gs.info('RCC Inbound Store is = ' + storeIs);

  current.store_number = storeIs;

Any ideas on how to add in the leading zeros?

1 ACCEPTED SOLUTION

Brian Dailey1
Kilo Sage

Hi Steven,



Try this to pad your store number within your script:



//Pad the value of storeIs to ensure it is 4-digits


for(i = storeIs.length;i < 4;i ++){


storeIs = '0' + storeIs;


}




That should work for you.   I ran into the same issue with an old Employee Number field, this was my solution.   Let us know if you still have any issue.




Thanks,


-Brian


View solution in original post

4 REPLIES 4

Deepa Srivastav
Kilo Sage

Sorry but i didnt get your question, you mean your inbound mail contains Store Epos:1 but by using the above code you get 0001 , is it?


Hi Deepa



Yes, it would need to be 0001 for a 1 digit number, 0021 for 2 digit, 0212 for the 3 digit number.



Steve


Brian Dailey1
Kilo Sage

Hi Steven,



Try this to pad your store number within your script:



//Pad the value of storeIs to ensure it is 4-digits


for(i = storeIs.length;i < 4;i ++){


storeIs = '0' + storeIs;


}




That should work for you.   I ran into the same issue with an old Employee Number field, this was my solution.   Let us know if you still have any issue.




Thanks,


-Brian


Thanks Brian. Worked like a treat.