Print Page | Close Window

SUBSTRING AND SUBSTR FUNCTION PROBLEMS

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: JavaScript
Forum Description: Discussions relating to the use of JavaScript within PDF.
URL: http://forums.planetpdf.com/forum_posts.asp?TID=2009
Printed Date: 27 Jun 2019 at 4:29am
Software Version: Web Wiz Forums 11.10 - http://www.webwizforums.com


Topic: SUBSTRING AND SUBSTR FUNCTION PROBLEMS
Posted By: lhasha
Subject: SUBSTRING AND SUBSTR FUNCTION PROBLEMS
Date Posted: 02 Jun 2016 at 7:31pm
Greetings:

I suspect this is just one of those times when the hidden issue is actually obvious but I need help none-the-less. And I thank all in advance. 

I CANNOT extract the last three characters of an eight character string. Question

Here is my code. I have commented it for clarity purposes:

001 // **** GENERATE NEW INVOICE NUMBER ****
002 // ---------------------------------------------------------------------------------------------------------------------
003 // This code is located in the 'Mouse Up' event of a button controllers
004
005 // Constructs the invoice number preface based upon the current year and month with a '1' preceding ("1" + "16" + "06")
006 var invPreface = "1" + util.printd ("yy", new Date()) + util.printd ("mm", new Date());
007
008 var lInv = this.getField("lastInvNo").value; // This is a hidden text field with a current value of: 11606001 (VERIFIED)
009 var InvNo = this.getField("InvoiceNumber").value; // This is the visible text field which displays the invoice number.
010 // Invoice numbers are ALWAYS 8 characters long
011
012 // If there is no previous invoice number
013 if (this.getField("lastInvNo").value == ""){ // Creates the first invoice number if none exists
014 this.getField("lastInvNo").value = invPreface + "001";
015 this.getField("InvoiceNumber").value = this.getField("lastInvNo").value;
016
017 // Otherwise increment last invoice number
018 }else{
019 var getSequence;
020 getSequence = lInv.substring(5,7); // I need the last three characters on lInv. I have also tried lInv.substring(5)
021 // and lInv.substr(5,3)
022
023 this.getField("Test").value = getSequence; // Used only during development and unit testing. Successfully shows
024 // lInv without substring or substr functions.
025
026 app.beep(); // Used as a pass through test alert. Successfully beeps when lInv is without substring or substr
027 // functions otherwise fails.
028
029 // Once the last three characters are retrieved, this value (001-998) will be incremented
030 // by 1. Then the results on this increase in value, the new value is add to the string
031 // "00" and then the last three characters of this string is add to the preface ("11606"
032 // + "002") thus creating the new invoice number (11606002).
034
035 // ISSUE: Regardless of what I try or how I construct the above "getSequence =" using the
036 // substring or substr functions, the value of 'getSequence' does not change.
039 // 
040 // I WOULD APPRECIATE ANY HELP IN SOLVING THIS ISSUE!
041
042 }

AGAIN, I WOULD APPRECIATE ANY HELP! Handshake


-------------
Greatly Appreciated!
Larry Bear



Replies:
Posted By: BAlheit
Date Posted: 03 Jun 2016 at 7:45am
Use this:

lInv.substr(-3)



Posted By: lhasha
Date Posted: 03 Jun 2016 at 6:07pm
THANKS FOR THE HELP !

BAlheit:

I had no idea that the substr could be notated that way. However, when it did not work in my instance, I revisited my code. Perhaps it should have been obvious to me before but it just hit me - this is a variable type issue. lInv = this.getField("lastInvNo").value sees the field value as an integer and not a string and, for obvious reasons, cannot extract the last three digits as a string.
    
  var lInv = ((this.getField("lastInvNo").value).toString()).substr(-3);
    
Does do the trick!!!! Beer

Thanks again for the kick (re) start!!! Thumbs Up



-------------
Greatly Appreciated!
Larry Bear


Posted By: gkaiseril
Date Posted: 04 Jun 2016 at 6:01pm
You could have also used the String constrictor.
 
var lInv = String((this.getField("lastInvNo").value).substr(-3);


Posted By: lhasha
Date Posted: 04 Jun 2016 at 10:40pm
COOL!

-------------
Greatly Appreciated!
Larry Bear



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 - http://www.webwizforums.com
Copyright ©2001-2017 Web Wiz Ltd. - https://www.webwiz.net