Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JavaScript trim(), trimLeft() function

Posted on 2016-09-30
12
Medium Priority
?
71 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 1800 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 59

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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 35

Expert Comment

by:ste5an
ID: 41823219
And consider using jQuery.
1
 

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 59

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 59

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

705 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