...

View Full Version : Javascript lookup help!



donster
06-09-2004, 07:28 PM
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!!!

sidvorak
06-09-2004, 07:53 PM
Donster,

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



<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>

Philip M
06-09-2004, 07:56 PM
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!

glenngv
06-10-2004, 05:49 AM
Using switch is more efficient as it doesn't need to execute each condition like if does.

donster
06-10-2004, 07:05 PM
THanks so much!!! I apologize for the lack of manners! I hope Phillip will cut me some slack since that I am a rookie :D

liorean
06-10-2004, 07:59 PM
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.)
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!';



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum