SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Validation?
-
May 26, 2002, 16:23 #1
- Join Date
- Nov 2001
- Location
- London, UK
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Validation?
Hi, I've had to make a simple script to do simple calculations:
Code:<script language=JavaScript> function calculate() { document.calculator.answer.value= eval(document.calculator.expression.value) } </script> <form name="calculator"> <input type="text" size="20" name="expression"> <input type="button" value="Calculate" onclick="calculate()"> <input type="reset" value="Reset"> Answer:<input type="text" size="20" name="answer"> </form>
I've tried to figure this out and searched everywhere on the net, but haven't found much! So is there a simple solution to this?
Thanks.
-
May 26, 2002, 18:20 #2
- Join Date
- Jan 2002
- Location
- London
- Posts
- 3,509
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This is best approached using regular expressions. See these to get started (plenty more where that came from here):
http://www.oreillynet.com/pub/a/java...id.html?page=1
and
http://www.siteexperts.com/tips/func...ts23/page1.asp
M@rco
-
May 26, 2002, 18:50 #3
- Join Date
- Nov 2001
- Location
- London, UK
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks M@rco, I've had a quick look at the links, but I'll try & work it out tomorrow now. I've had enough of staring at the computer screen for today!
-
May 27, 2002, 15:38 #4
- Join Date
- Nov 2001
- Location
- London, UK
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've finally come up with the following, it seems ok:
Code:<html> <head> <title>Using The Eval() Function</title> <script> <!-- function calculate() { document.calculator.answer.value = eval(document.calculator.expression.value) } function checkInput(strng) { var illegalChars= /[\a-z\A-Z\!\"\£\$\%\^\&\=\{\}\[\]\;\:\'\@\#\~\,\<\>\\\?\|\`]/; if (strng.match(illegalChars)) { alert("Invalid Expression Entered!"); document.calculator.expression.focus(); document.calculator.expression.select(); return false } return true } function focus() { document.calculator.expression.focus(); } // --> </script> </head> <body onload=focus()> <basefont=3> <font face=verdana size=-1> <center> <b>Using The Eval() Function</b> </center> </font> <p> <font face=verdana size=-2> <form name="calculator"> Please enter a valid mathematical expression: <input type="text" size="15" name="expression"> <input type="button" value="Calculate" onClick="if (checkInput(document.calculator.expression.value)) { calculate(this.form) }"> <input type="reset" value="Reset"> Answer = <input type="text" size="15" name="answer"> </form> </font> </body> </html>
-
May 27, 2002, 17:58 #5
- Join Date
- Jan 2002
- Location
- London
- Posts
- 3,509
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You're welcome! Handy, aren't they? (RegExs)
M@rco
-
May 27, 2002, 18:14 #6
- Join Date
- Nov 2001
- Location
- London, UK
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yeah they are, I've come across them before, but didn't know what they were called & didn't really know how to use them...
So, I guess everything seems allright in my scripts above?
-
May 28, 2002, 04:25 #7
- Join Date
- Jan 2002
- Location
- London
- Posts
- 3,509
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It seems fine (only had a quick play because I'm in the middle of my Uni finals)!
M@rco
-
May 28, 2002, 14:08 #8
- Join Date
- Nov 2001
- Location
- London, UK
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Cool. Good Luck with your exams (I HATE exams).
Bookmarks