Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP - Allow only one particular word or any number

    I am trying to edit a script to add some functionality.

    I want to check a form input field and throw an error if it's blank or if it contains anything other than the word "Unlimited" or a number.

    Here's a snippit but I know it's not correct:

    PHP Code:
    $use $_POST['uses'];
    if (!
    $edtErr) {
    if ((!
    $use)||($use!="Unlimited")&&(!ctype_digit($use)) {                         
    $edtErr true;                  

    Anyone care to help me fix this

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $use $_POST['uses'];

    if ( 
    'Unlimited' != $use && ! ctype_digit$use ) )
    {
        
    $edtErr TRUE;

    ...or:
    PHP Code:
    $use $_POST['uses'];

    $edtErr = ( 'Unlimited' != $use && ! ctype_digit$use ) ); 
    Last edited by kbluhm; 06-26-2012 at 04:30 PM.

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks kbluhm but that code actually did not work for me.

    It would accept anything with a number in it.

    I ended up using this:

    PHP Code:
    if ((!$use ) || ($use != 'unlimited') && (!preg_match("/^\d+$/",$use )) ) { 


  •  

    Tags for this Thread

    Posting Permissions

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