Solved

JavaScript trim(), trimLeft() function

Posted on 2016-09-30
12
23 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 52

Accepted Solution

by:
Julian Hansen earned 450 total points
ID: 41823183
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 52

Expert Comment

by:Julian Hansen
ID: 41823184
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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 50 total points
ID: 41823185
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
ID: 41823214
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
ID: 41823216
sorry mistake... doesn't

debugging now
0
 
LVL 33

Expert Comment

by:ste5an
ID: 41823219
And consider using jQuery.
1
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Darius
ID: 41823226
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 52

Expert Comment

by:Julian Hansen
ID: 41823227
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
ID: 41823231
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
ID: 41823242
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
ID: 41823246
ste5an,

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

Expert Comment

by:Julian Hansen
ID: 41823248
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery scrollTop 2 41
Webpage error - Unable to get property "apply" of undefined or null reference 4 33
Split in Javascript 5 31
Jquery get each value 2 15
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

896 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

13 Experts available now in Live!

Get 1:1 Help Now