SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Validation?

Hybrid View

  1. #1
    SitePoint Enthusiast
    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>
    But, what I now want to do is validate it, so that if any letters of the alphabet are entered > then an alert comes up. So basically, all I want allowed is: numbers + - * / . ( )

    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.

  2. #2
    The doctor is in... silver trophy MarcusJT's Avatar
    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

  3. #3
    SitePoint Enthusiast
    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!

  4. #4
    SitePoint Enthusiast
    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>
    Thanks again for the help M@rco.

  5. #5
    The doctor is in... silver trophy MarcusJT's Avatar
    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

  6. #6
    SitePoint Enthusiast
    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?


  7. #7
    The doctor is in... silver trophy MarcusJT's Avatar
    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

  8. #8
    SitePoint Enthusiast
    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

Posting Permissions

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