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 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I really need some help with Javascript for a possible project. Thank You ahead of it

    So I feel like I should just explain what I am looking to do. Lets say there is an Item that has the number 001234-A1. This Number is the exact number that the manufacturer lists it is, however whenever it gets placed into other catalogs or websites the item number changes slightly for example:
    • 12-34a1
    • 0001234A1
    • 1-234-A1
    • 123-4a1

    So what I would like to have someone show me how to do it write a script that can do three things
    • Remove all leading zero's
    • Remove all hyphens
    • Make all letters uppercase.

    This way we now have a normalized number (1234A1).
    So to recap I need to create a string that takes any different numbers, normalizes those numbers, and then matches them with all other matching numbers.
    I really would appreciate some assistance in this. Thank You, Thank You

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    - remove leading zeroes: use String.replace() with a RegExp
    - remove hyphens: same function, though no RegExp required
    - use String.toUpperCase()
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Easy.

    Code:
    function AlphaNumOnly( str )
    {
        return str.replace(/[^a-z0-9]/g,"").toUpperCase();
    }
    
    alert( AlphaNumOnly("     12-34a1    ") ); // with spaces
    alert( AlphaNumOnly("0001234A1") );
    alert( AlphaNumOnly("$.@!&*;:1-234-A1") ); // other characters thrown in
    alert( AlphaNumOnly("123-4a1") );
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Old Pedant View Post
    Easy.

    Code:
    function AlphaNumOnly( str )
    {
        return str.replace(/[^a-z0-9]/g,"").toUpperCase();
    }
    
    alert( AlphaNumOnly("     12-34a1    ") ); // with spaces
    alert( AlphaNumOnly("0001234A1") );
    alert( AlphaNumOnly("$.@!&*;:1-234-A1") ); // other characters thrown in
    alert( AlphaNumOnly("123-4a1") );
    Needs slight improvement to strip leading zeroes as reqested:-
    Code:
    function AlphaNumOnly( str ) {
    str = str.replace(/[^0-9a-z]/gi,"").toUpperCase();
    str = str.replace(/^0+/,"");
    return str;
    }

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Told you I'm going blind as well as senile. Flat out missed that requirement.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Dec 2011
    Location
    Earth if you want to know
    Posts
    27
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Philip M View Post
    Needs slight improvement to strip leading zeroes as reqested:-
    Code:
    function AlphaNumOnly( str ) {
    str = str.replace(/[^0-9a-z]/gi,"").toUpperCase();
    str = str.replace(/^0+/,"");
    return str;
    }
    Hi,

    That won't work if the item has special characters, he didn't say that special characters were not allowed. So let's do what he said:

    Code:
    function format($string) {
        //Remove all leading zeros
        while (parseInt($string[0]) === 0) {
            $string = $string.substr(1, $string.length - 1);
        };
    
        //Remove all hyphens
        $string = $string.split("-").join("");
    
        //Make all letters uppercase
        return $string.toUpperCase();
    };
    Test:

    Code:
    var $code = "000123-4A-1";
    document.write(format($code));
    Result:

    Code:
    1234A1
    I hope this help.

    Tim_

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by cuzMazn View Post
    Hi,

    That won't work if the item has special characters, he didn't say that special characters were not allowed. So let's do what he said:
    My understanding of the OP's post #1 is that the manufacturer's item code consists of letters, numbers and hyphens. Special characters and spaces were not mentioned. Perhaps the OP can confirm. But if that is really what is wanted then only a slight alteration to the code is needed.


    Code:
    function AlphaNumOnly( str ) {
    str = str.replace(/-/gi,"").toUpperCase();  // strip hyphens only, not spaces or special characters
    str = str.replace(/^0+/,"");
    return str;
    }

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    I would even go as far as proposing
    PHP Code:
    function AlphaNumOnlystr ) {
        return 
    str.replace(/^0+/, "").replace("-""").toUpperCase();

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Dormilich View Post
    I would even go as far as proposing
    PHP Code:
    function AlphaNumOnlystr ) {
        return 
    str.replace(/^0+/, "").replace("-""").toUpperCase();

    Try
    Code:
    alert (AlphaNumOnly("----0000abc123"));  // ---ABC123
    (just strips one hyphen)

    Code:
    function AlphaNumOnly2( str ) {
    str = str.replace(/-/gi,"").toUpperCase();  // strip hyphens only, not spaces or special characters
    str = str.replace(/^0+/,"");
    return str;
    }
    
    alert (AlphaNumOnly2("----0000abc123"));  // ABC123
    Last edited by Philip M; 12-17-2011 at 12:24 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    my bad, just looked it up, last time I used .replace("-", "", "g") which is equivalent to using a RegExp.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Still does not wok. And I have to confess that I am not familiar with that notation.

    Code:
    return str.replace(/^0+/, "").replace(/-/g, "").toUpperCase();  // 0000ABC1123
    One-liners may be elegant but are harder to understand, and prone to errors.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    depends on which string you use. if you take ----0000abc123 then it won’t work unless you first strip the hyphens, which will make then the zeroes leading (otherwise they are in the middle), hence using str.replace(/-/g, "").replace(/^0+/, "").toUpperCase() succeeds.

    regarding that one-liner, it’s just chaining, nothing more, nothing less.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #13
    New Coder
    Join Date
    Dec 2011
    Location
    Earth if you want to know
    Posts
    27
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Dormilich View Post
    depends on which string you use. if you take ----0000abc123 then it won’t work unless you first strip the hyphens, which will make then the zeroes leading (otherwise they are in the middle), hence using str.replace(/-/g, "").replace(/^0+/, "").toUpperCase() succeeds.

    regarding that one-liner, it’s just chaining, nothing more, nothing less.
    Hi,

    I don't think that is correct. If I understand the description, it means that the zeros in "---000abc123" is no longer leading zeros, they are now part of the code. Beeginger should clarify it.

    Tim_

  • #14
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by cuzMazn View Post
    I don't think that is correct. If I understand the description, it means that the zeros in "---000abc123" is no longer leading zeros, they are now part of the code. Beeginger should clarify it.
    The OP states that this is about normalizing different number formats that treat hyphens as non-semantic, so hyphens are clearly not allowed to change leading- to non-leading-zeroes.

    Some catalog might choose to represent 001234-A1 as 0-0-1234-A1, but the normalized form is still supposed to be 1234A1, and not 01234A1.

    And since we're having fun with hard to maintain regexps, here's mine:
    Code:
    str.replace(/^[0-]*|-/g, '').toUpperCase();
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback


  •  

    Posting Permissions

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