SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: number ONLY

  1. #1
    SitePoint Enthusiast webmasts's Avatar
    Join Date
    Apr 2003
    Location
    usa
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    number ONLY

    Hi,

    How can I have people only enter NUMBERS not letters or other symbols in my php text box?

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    vta,ca,usa
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume you mean 'within a form text box that will be submitted to a PHP script'.

    The script that handles the form would have some logic to test for the data type that has been input for a specific variable, e.g. using the PHP function is_numeric() and related functions. You can also use a function like strlen() to verify the string is within a certain length (2 characters rather than 100 characters, for example).

    Obviously this method only checks the value once the form was submitted; in order to make sure the user enters the correct data prior to the form submission, you want to use Javascript.

  3. #3
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The best way is to use Regular Expressions: (They're not that scary! )

    let's say the name of your form field is "total", should only be an integer (a whole number) and the form's method is a "post":

    Receiving (form processing) script:
    PHP Code:
    $regex "[a-z]|[A-Z]";
    // If non-numeric characters are found, echo a warning message:
    if(preg_match("#$regex#",$_POST['total']))
    {
    echo 
    "No letter characters allowed!";
    }
    // Else allow the submit
    else
    {
    // Do some other stuff like insert into database or have a cup of tea...

    Of course you can expand upon that regular expression in the $regex variable, to check and dissallow characters and symbols and the like.

    Check out the manual at:http://uk2.php.net/manual/en/ref.pcre.php

    HTH

  4. #4
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd disagree with phptek only because your question is pretty basic and simple in it needs. PHP has built in functions as bdl points out (is_numeric) that make it easier than worring about regular expressions when all you need is to check that the input is a number. Now if you need to do more advanced validation then regular expressions are invaluable and while they are complicated to look at for the newbie, once you've picked up some basics then they become friendlier.
    Erh

  5. #5
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use is_numeric() most of the times for simple checking purposes.

    A simple example would be...

    PHP Code:
        if (!is_numeric($_POST['var'])) {

            echo(
    "Sorry only numbers are allowed");
            exit;
        } 
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  6. #6
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by webmasts
    Hi,

    How can I have people only enter NUMBERS not letters or other symbols in my php text box?
    If you want to check it in Javascript before you even submit your form (you can do this in addition to the PHP validation, but at the very least you should do the server-side validation), try this:
    HTML Code:
    <input type="text" name="number" onblur="if (isNaN(this.value)) alert('Please enter only numbers in this field'); return false;">
    You can pretty it up in a JS function too if you want.


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
  •