SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Text field to require 1 specific number

    Hi, I have a simple form with a sum.

    Sum is 4 + 1 = ... the answer will of course be 5.

    Does any one know of a simple tutorial where a specific number can be entered into a text field. If number is incorrect it pops up a simple error box.

    Any help would be great. Thanks

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code HTML4Strict:
    <form action="..." id="my-form">
      <div>
        <label for="sum">Sum is 4 + 1 =</label>
        <input type="text" id="sum">
        <input type="submit" value="Submit">
      </div>
    </form>
    Code JavaScript:
    var form = document.getElementById("my-form");
    form.onsubmit = function () {
        var sum = document.getElementById("sum");
        if (sum.value !== "5") {
            alert("Wrong answer!");
            return false;
        }
        return true;
    };
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked an absolute charm, thank you very much


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
  •