Solved

JavaScript trim(), trimLeft() function

Posted on 2016-09-30
12
17 Views
Last Modified: 2016-09-30
Hi guys,
  I tried to use  trimLeft('0') function but I getting error:
JavaScript runtime error: Object doesn't support property or method 'trimLeft'

. . . 
        else if (invoiceId.trim().trimLeft('0').length > 10) {
            warn = "!" + "Invoice ID has more then 10 characters.\n";
            document.body.style.cursor = 'default';
            document.getElementById("btnSend").onclick = '';
        }
. . .

Open in new window


// Original Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/javascript" src="inc/src/datePicker.js" language="javascript"></script>
<script type="text/javascript">

// input:      invoiceId  = "  001234567890   "
. . .

        else if (invoiceId == "") {
            warn = "!" + "Please, enter the Invoice ID.\n";
            document.body.style.cursor = 'default';
            document.getElementById("btnSend").onclick = '';
        }
. . .

// if fields not empty, then
// Removes all leading and trailing white-space characters from the string and then
// Removes all leading occurrences of the specified character 0 (zeros)
        else if (invoiceId.length > 10) {
            warn = "!" + "Invoice ID has more then 10 characters.\n";
            document.body.style.cursor = 'default';
            document.getElementById("btnSend").onclick = '';
        }

// required success output: invoiceId = "1234567890"

Open in new window


Any ideas?
Thank You
0
Comment
Question by:Darius
12 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 450 total points
Comment Utility
trimLeft is a non-standard JS function - you should not use if if you want your code to work across all browsers.
Trim does not accept a value - it just trims white space.

If you want to remove leading zeros you can just convert it to an integer

var str = '0000123';
console.log(parseInt(str));

Open in new window

1
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Relating to your code
var invoiceId = '000012345678987';
var invoiceNum = parseInt(invoiceId);
if (invoiceNum > 999999999) {
  console.log('invoice ID > 10');
}
else {
  console.log('Invoice ID is Ok!');
}

Open in new window

0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 50 total points
Comment Utility
This page https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/TrimLeft says it was a non-standard function and is deprecated.  It is also not supported by any other browser.
1
 

Author Comment

by:Darius
Comment Utility
Thank you Julian, Dave

it works:

invoiceId = parseInt(document.getElementById("txtMessageId").value);
. . .
else if (console.log('invoice ID > 10')) {
            warn = "!" + "Invoice ID has more then 10 characters.\n";
            document.body.style.cursor = 'default';
            document.getElementById("btnSend").onclick = '';
        }

Open in new window

0
 

Author Comment

by:Darius
Comment Utility
sorry mistake... doesn't

debugging now
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
And consider using jQuery.
1
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Darius
Comment Utility
Julian is this correct way to do?
never reaching 'warning' if value more than 10 char...

invoiceId = parseInt(document.getElementById("txtMessageId").value);
. . .

else if (console.log('invoiceId > 10')) {
            warn = "!" + "Invoice ID has more then 10 characters.\n";
            document.body.style.cursor = 'default';
            document.getElementById("btnSend").onclick = '';
        }

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
else if (console.log('invoice ID > 10')) {

Open in new window

????
What are you trying to do with this statement ?
1
 

Author Comment

by:Darius
Comment Utility
Sorry Julian,
my mistake it works

invoiceId = parseInt(document.getElementById("txtMessageId").value);
. . .
else if (invoiceId > 10) {
            warn = "!" + "Invoice ID has more then 10 characters.\n";
            document.body.style.cursor = 'default';
            document.getElementById("btnSend").onclick = '';
        }
0
 

Author Comment

by:Darius
Comment Utility
Thank you very much guys...

Julian thanks for your time, It's my first days with JavaScript even English is not a first language(fourth). I feel a little bit uncomfortable when I need to explain...
0
 

Author Comment

by:Darius
Comment Utility
ste5an,

I agree with you, but this is really old project and I not allowed to make changes like this...
Thank you
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are most welcome - we were all there at some point with JS and hats off to you on the English front - I think sometimes we English speakers take it for granted.
I feel a little bit uncomfortable when I need to explain...
I hope that we can make it feel more comfortable for you in the future.
1

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now