Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 84
  • Last Modified:

JavaScript trim(), trimLeft() function

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
Darius
Asked:
Darius
2 Solutions
 
Julian HansenCommented:
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
 
Julian HansenCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DariusAuthor Commented:
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
 
DariusAuthor Commented:
sorry mistake... doesn't

debugging now
0
 
ste5anSenior DeveloperCommented:
And consider using jQuery.
1
 
DariusAuthor Commented:
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
 
Julian HansenCommented:
else if (console.log('invoice ID > 10')) {

Open in new window

????
What are you trying to do with this statement ?
1
 
DariusAuthor Commented:
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
 
DariusAuthor Commented:
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
 
DariusAuthor Commented:
ste5an,

I agree with you, but this is really old project and I not allowed to make changes like this...
Thank you
0
 
Julian HansenCommented:
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now