Solved

JavaScript trim(), trimLeft() function

Posted on 2016-09-30
12
43 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 57

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 57

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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 34

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 57

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 57

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Node.js 11 82
arrays and buttons with user input 2 42
15 puzzle in Javascript/html 2 89
Make a span connected to the price so when qty changes so does pricing 11 44
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

751 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