www.webdeveloper.com
Results 1 to 2 of 2

Thread: form input field that only accepts numbers and decimal points

  1. #1
    Join Date
    Jan 2003
    Posts
    7

    form input field that only accepts numbers and decimal points

    form input type that only accepts nu,mbers and decimal points if anyone got some code id really appreciate it
    roory

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <title>Form Example</title>
    <form action="foo.pl">
    <p>
    <label for="number">Number:</label>
    <br>
    <input id="number" type="text" onchange="if (/[^\d\.]/.test(this.number.value) {alert('This field requires a Number'); this.value=''; this.focus()}">
    </p>
    <p>
    <input type="submit">
    </p>
    </form>

    But like all JavaScript, this will fail for a great number of users. In this case about 12% of them.

    And if you want something that tests more specifically for a number (id est only one decimal point, negative numbers permitted) then this might do the trick:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <title>Form Example</title>
    <form action="foo.pl">
    <p>
    <label for="number">Number:</label>
    <br>
    <input id="number" type="text" onchange="if (/^\.?$/.test(this.value) || !/^-?\d*\.?\d*$/.test(this.value)) {alert('This field requires a Number'); this.value=''; this.focus()}">
    </p>
    <p>
    <input type="submit">
    </p>
    </form>
    Last edited by Charles; 01-31-2003 at 06:26 AM.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles