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 07-14-2008, 11:54 AM   PM User | #1
Snetty
New Coder

 
Join Date: May 2005
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
Snetty has a little shameless behaviour in the past
Simple Regex problem :(

I'm trying to write a regular expression that validates a form entry. It's designed to only allow uppercase alphabetic characters and numbers, it must also be between 2 and 4 characters in length (inclusive).

I came up with the following.. but it's allowing pretty much everything through, can anyone see where I'm going wrong?

PHP Code:
if(preg_match('/^([A-Z0-9]{2,4})$/'$_POST['prefix']) === FALSE){ fail 
Any help much appreciated.
Snetty is offline   Reply With Quote
Old 07-14-2008, 12:01 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
See http://in.php.net/preg_match
Quote:
Return Values

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject . preg_match() returns FALSE if an error occurred.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 07-14-2008, 12:22 PM   PM User | #3
Iszak
Regular Coder

 
Iszak's Avatar
 
Join Date: Jun 2007
Location: Perth, Western Australia
Posts: 332
Thanks: 2
Thanked 58 Times in 57 Posts
Iszak is an unknown quantity at this point
PHP Code:
<?php

if (!preg_match('/^[A-Z0-9]{2,4}$/'$_POST['prefix'))
{
  echo 
"Invalid";
}

?>
Regards
Iszak is offline   Reply With Quote
Old 07-14-2008, 05:31 PM   PM User | #4
Snetty
New Coder

 
Join Date: May 2005
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
Snetty has a little shameless behaviour in the past
excellent, thanks for the help guys. I used to use eregi and I guess I just didn't think to check how preg_match deals with not finding a match.
Snetty is offline   Reply With Quote
Reply

Bookmarks

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 09:37 AM.


Advertisement
Log in to turn off these ads.