Results 1 to 6 of 6

Thread: <input type="text" to accept numbers only, its possible?

  1. #1
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default <input type="text" to accept numbers only, its possible?

    Hello, as the title says i want to have on a form a "text" field to accept only numbers (0-9) is this possible? i hope someone could answer me fast, i need to finish it today o.0 Thanks!

  2. #2
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Why not use <select> instead?

    HTML Code:
    <form>
    <select>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    </select>
    </form>

  3. #3
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Hello, thanks for your fast repy, i can't use select because the number values are from 0 till 99999, its a points adder (calculator machine) and it would make the code real heavy, can you imagine 99999 of those <option? hehe

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    You could use javascript to see if a number has been entered in the textbox. If you go this route though; and you are pointing the form action to a server-side script, make sure you have a backup test to see if the data submitted was a number or if it was text (just in case the user has javascript disabled for whatever reason).

    Anyways; as for the code for this "check", I think you could do something like the following:

    Place between head tags
    Code:
    <script type="text/javascript">
    
    function doCheck(field) {
     if (isNaN(document.getElementById(field).value)) {
        alert('This is not a number! Please enter a valid number before submitting the form.');
        document.getElementById(field).focus();
        document.getElementById(field).select(); 
        return false;
     }
    
     else {
        return true;
     }
    }
    Use as template for form:

    Code:
    <form action="something.php" method="POST" onsubmit="return doCheck('number');">
    
    <input type="text" name="something" id="number">
    <input type="submit" value="Check Form">
    </form>
    Not tested, but should work. Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Hello, when the user inserts a letter(s) instead of numbers and press submit, it gives the congratulations message like if the script were ran correctly but in fact nothing were added into the db, i want o change the message without using javascript if possible, now i was cheching this function but i can't make it work.

    $str = (int)str_replace("'","",stripslashes($_POST[strength]));

    if (!ereg("([0-9]+)",$str)) { print_error("Only numbers are allowed."); }

    It doesn't work and i can't see whats wrong (im noob)

  6. #6
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    nevermind i found the solution myself:

    $str = $_POST[strength];
    if ( eregi( "[a-z]", $str ) ) { print_error("Numbers only!."); }

    now the problem is with the wierd symbols...

    EDIT: WHOAH! PROBLEM FIXED! :]

    if ( !eregi( "^[0-9]+$", $str ))

    so if they write anything except numbers it will show the error and wont try to save on db. hehe, thanks you two for your reply!.
    Last edited by nicksalad; 06-22-2007 at 05:38 PM.

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
  •