Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-26-2012, 02:44 PM   PM User | #1
jeffshead
New Coder

 
Join Date: Oct 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
jeffshead is an unknown quantity at this point
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
jeffshead is offline   Reply With Quote
Old 06-26-2012, 03:28 PM   PM User | #2
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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 ) ); 
__________________
ZCE

Last edited by kbluhm; 06-26-2012 at 03:30 PM..
kbluhm is offline   Reply With Quote
Old 07-09-2012, 07:18 PM   PM User | #3
jeffshead
New Coder

 
Join Date: Oct 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
jeffshead is an unknown quantity at this point
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 )) ) { 
jeffshead is offline   Reply With Quote
Reply

Bookmarks

Tags
alpha, input, numbers, numeric, regex

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:20 PM.


Advertisement
Log in to turn off these ads.