OZONE Asylum
Forums
DHTML/Javascript
Converter from String
This page's ID:
28076
Search
QuickChanges
Forums
FAQ
Archives
Register
Edit Post
Who can edit a post?
The poster and administrators may edit a post. The poster can only edit it for a short while after the initial post.
Your User Name:
Your Password:
Login Options:
Remember Me On This Computer
Your Text:
Insert Slimies »
Insert UBB Code »
Close
Last Tag
|
All Tags
UBB Help
I am also trying to do a converter using a string input. e.g.: How many meters in 5 miles? It will pull the "meters" the "5" and the "miles" from the string, do the math, and return the output. [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> [/code] [small](Edited by [url=http://www.ozoneasylum.com/user/6200]Hutch[/url] on 06-14-2006 00:22)[/small]
Loading...
Options:
Enable Slimies
Enable Linkwords
« Backwards
—
Onwards »