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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript lookup help!

    I currently have a form that when a user enters a serial number in the first textbox named “code” . The script will look up the year of manufacture and then display the result in a second textbox named “codeResult”

    Example:
    function getYear(code) {

    code = code.toUpperCase();
    switch (code) {

    case "1150": code = "1903"; break;
    case "1850": code = "1904"; break;
    case "2550": code = "1905"; break;
    case "3350": code = "1906"; break;
    case "4250": code = "1907"; break;
    case "5450": code = "1908"; break;
    case "6950": code = "1909"; break;
    case "8750": code = "1910"; break;
    case "10850": code = "1911"; break;
    case "13350": code = "1912"; break;

    default: code = "No record!";
    }
    document.code.codeResult.value = code;
    }


    <form name="code">
    <input type="text" size="10" maxlength="10" name="code"><b>&laquo;</b>
    Enter Your Gibson guitar's Serial Number
    <Input TYPE="button" VALUE="Lookup Year" onclick="getYear(document.code.code.value);">
    <input type="text" size="10" value="" name="codeResult"> <b>&laquo;</b> Result
    </form>

    how can I code this to achieve the following scenerio:

    if the serial number entered falls between:
    100 and 1150 the year must equal 1903
    1151 to 1850 would equal 1904 and so on
    How can the code be modified to achieve this criteria?



    Thanks in advance!!!

  • #2
    New Coder
    Join Date
    Mar 2004
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Donster,

    Here you go! Changed a couple things.
    Cheers,
    Simon

    Code:
    <script type="text/javascript">
    function getYear(code) {
    
    //code = code.toUpperCase();
    switch (true) {
    
    case (code>=100)&&(code<=1150): code = "1903"; break;
    case (code>=1151)&&(code<=1850): code = "1904"; break;
    case (code>=1851)&&(code<=2550): code = "1905"; break;
    case (code>=2551)&&(code<=3350): code = "1906"; break;
    case (code>=3351)&&(code<=4250): code = "1907"; break;
    case (code>=4251)&&(code<=5450): code = "1908"; break;
    case (code>=5451)&&(code<=6950): code = "1909"; break;
    case (code>=6951)&&(code<=8750): code = "1910"; break;
    case (code>=8751)&&(code<=10850): code = "1911"; break;
    case (code>=10851)&&(code<=13350): code = "1912"; break;
    
    default: code = "No record!";
    }
    document.code.codeResult.value = code;
    }
    </script>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    I guess the simplest is to re-work it thus:

    var year = 0
    if (code >=100 && code <= 1150) {year = 1903}
    if (code >=1151 && code <=1850) {year = 1904}
    ... and so on

    if (year ==0) {year="No record"}


    Another newcomer offering "thanks in advance". Yuck! Puke!
    Manners maketh the man!

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Using switch is more efficient as it doesn't need to execute each condition like if does.

  • #5
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you all

    THanks so much!!! I apologize for the lack of manners! I hope Phillip will cut me some slack since that I am a rookie

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Why not do it all in one go instead? (You probably don't want the code indented like that, but I used it to clarify levels of conditionals in the example.)
    Code:
    var
        code=(code<100)?
            'No record!':
            code<1151?
                '1903':
                code<1851?
                    '1904':
                    code<2551?
                        '1905':
                        code<3351?
                            '1906':
                            code<4251?
                                '1907':
                                code<5451?
                                    '1908':
                                    code<6951?
                                        '1909':
                                        code<8751?
                                            '1910':
                                            code<10851?
                                                '1911':
                                                code<13351?
                                                    '1912':
                                                    'No record!';
    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


  •  

    Posting Permissions

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