Topic: Converter from String (Page 1 of 1) |
|
---|---|
Obsessive-Compulsive (I) Inmate From: |
posted 06-14-2006 00:21
I am also trying to do a converter using a string input. e.g.: How many meters in 5 miles? code: <html> <head> <title>String Converter</title> <script language="Javascript" type="javascript/text"> function Convert (string1) { if (/\smeters\s.+\d+.*\smiles/i.test(string1)){ var num1=Number(/\d+/.exec(string1)); var num2=num1/0.0006214; document.form1.output.value=num1 +'miles is equal to' + num2 'meters.'; } if (/\sinches\s.+\d+.*\smeters/i.test(string1)){ var num1=Number(/\d+/.exec(string1)); var num2=num1/0.00254; document.form1.output.value=num1 +'meters is equal to' + num2 'inches.'; } if (/\scentimeters\s.+\d+.*\sinches/i.test(string1)){ var num1=Number(/\d+/.exec(string1)); var num2=num1/0.3937; document.form1.output.value=num1 +'inches is equal to' + num2 'centimeters.'; } if (/\scentimeters\s.+\d+.*\smiles/i.test(string1)){ var num1=Number(/\d+/.exec(string1)); var num2=num1/0.00000621371192; document.form1.output.value=num1 +'miles is equal to' + num2 'centimetres.'; } if (/\slitres\s.+\d+.*\squarts/i.test(string1)){ var num1=Number(/\d+/.exec(string1)); var num2=num1/1.05669; document.form1.output.value=num1 +'US quarts is equal to' + num2 'litres.'; } if (/\sgrams\s.+\d+.*\spounds/i.test(string1)){ var num1=Number(/\d+/.exec(string1)); var num2=num1/0.002205; document.form1.output.value=num1 +'miles is equal to' + num2 'meters.'; } else{ alert('Please enter a valid conversion'); document.form1.input.focus(); } } </script> </head> <body> This conversion tool accepts the following conversions:<br> meters -> miles<br> inches -> meters<br> centimeters -> inches<br> centimeters -> miles<br> litres -> quarts<br> grams -> pounds<br> in the form:<br> e.g. "How many inches in 5 meters?" <form name="form1" onsubmit="return false;"> Please enter the string you wish to convert: <input type="text" name="original"> <p> <input type="button" value="Convert!" onclick="Convert(document.form1.original.value);"> <p> <textarea name="output" cols=40 rows=25></textarea> </form> </body> </html>
|