Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing string starting with a zero

    I have a var called caldate which is made up of a day and a month ie

    0202 (2nd feb(yes i'm english ))

    the var is used to create a td id and then again as a param in a javascript call.

    The id is fine. The rollover for the call shows the correct info in the status bar, but the receiving script changes it!!

    example

    month_date = 01
    day = 1

    caldate="0" + day + month_date;// to give 0101
    document.write("<td id="+caldate+"><a href='javascript:hilite("+String(caldate)+",true);'>"+day+"</a></td>")

    when an alert is placed in the hilite script it gives 65!!!!!
    the 2nd jan gives 129. the 1st and 2nd of feb give 66 and 130 (this pattern continues for every month and every day up to the 9th)

    Where am i going wrong?
    Monkey

    My head hurts!

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    I am not to sure here but I see that you are passing a string concatenated to variables as an argument, maybe this is where the problem lies


    month_date = 01
    day = 1

    caldate="0" + day + month_date;// to give 0101

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about:
    Code:
    caldate="";
    caldate+="" + "0" + day + month_date;//
    Or something like that..

    Sadiq.

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    What about when the date is higher than the 9th? You'd then have a date as
    01002

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem you're seeing is that the other function (which you didn't show) is where the bug is. You're using the parseInt function to parse that number and because the number has a leading "0" the parseInt function is interpreting the input number as Octal (0202 octal = decimal 130). You need to add the "radix" argument to your parseInt function to force it to treat all input numbers as decimal numbers.

    So instead of myVar = parseInt(myString) use myVar = parseInt(myString,10)

    Adding that additional parameter will fix your problem nicely.

    Also please note that ID names cannot start with a number or be purely numeric and still be valid, the current crop of browsers may forgive this but there may come a point in time where you run into a browser that won't so you should plan on fixing that problem too.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #6
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Roy - woh, never realised numbers were so complicated, I thought reg exp was confusing!!

    Desided to look into the whole octal/radix thing and wish I hadn't! I've decided to get rid of the whole zero at the begining thing!

    Cheers for your help, I think this is one for a rainy day...or week!
    Monkey

    My head hurts!

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Browsers support IDs beginnning with digits in scripting, but you'll get problems when trying to style such elements using CSS.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cheers for the tip - doesnt make a difference in this case but i will remember it in the future. what about numbers converted to strings - will that work ok with css?
    Monkey

    My head hurts!

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Attributes are always strings, so that doesn't make any difference. The first character needs to be /[a-zA-Z_]/.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    parseInt is useful but you do need to be aware that you should always add that "radix" parameter unless you're delibarately expecting to process input numbers that could be octal, hex binary or decimal depending on the format of the number.
    Check out the Forum Search. It's the short path to getting great results from this forum.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •