SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: preg_match again !!
-
Jan 20, 2003, 12:07 #1
- Join Date
- Feb 2002
- Location
- NZ
- Posts
- 620
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
preg_match again !!
I'm trying to build a function that looks for:
1. One character in field.
2. A maximum of 20 characters
3. Illegal characters.
Now the problem I have is that the 2 preg_match's that I'm using dont allow illegal characters. So the message returned isn't correct. I want to be able to return a message using 1 of the above, I dont want a message saying "please fill in your First Name" if in fact illegal characters have been used.
Heres what I have compliments of forum users which were correct based on the questions I asked at the time.
PHP Code:
/* make sure field is filled in */
if(!preg_match("/\\w{1}/i", $name))
{
$feedback .= "Please fill in your First name";
}
/* make sure field is not over filled */
elseif(!preg_match("/^\\w{1,20}$/", $name))
{
$feedback .= "Maximum of 20 characters for your First name";
}
-
Jan 20, 2003, 12:40 #2
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Use this for your first match
Code:if( preg_match( "/^\S*$/", $name ) ) { $feedback .= "Please fill in your First name"; }
-
Jan 20, 2003, 13:14 #3
- Join Date
- Feb 2002
- Location
- NZ
- Posts
- 620
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You were close.
Heres the answer I came up with and I'm not saying its perfect by any means.
Code:/* make sure field is filled in */ if(!preg_match("/\S{1}/i", $name)) { $feedback .= "Please fill in your First name"; } /* make sure field is not over filled */ elseif(!preg_match("/^\S{1,20}$/", $name)) { $feedback .= "Maximum of 20 characters for your First name"; } /* Contains all legal characters */ elseif(preg_match("/\W{1}/i", $name)) { $feedback .= "Please remove the illegal characters from your First name"; }
Last edited by Motivated; Jan 20, 2003 at 13:17.
-
Jan 20, 2003, 13:21 #4
- Join Date
- Feb 2002
- Location
- NZ
- Posts
- 620
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
beetle any chance you can tell me where to put the returns in this function ???
Last edited by Motivated; Jan 20, 2003 at 14:27.
-
Jan 20, 2003, 14:44 #5
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sure, if I follow correctly...
PHP Code:function whatever( $name )
{
/* make sure field is filled in */
if ( preg_match( "/^\\s*$/i", $name ) )
{
$feedback .= "Please fill in your First name";
return false;
}
/* make sure field is not over filled */
elseif ( strlen( $name ) > 20 )
{
$feedback .= "Maximum of 20 characters for your First name";
return false;
}
/* Contains all legal characters */
elseif ( preg_match( "/\\W/i", $name ) )
{
$feedback .= "Please remove the illegal characters from your First name";
return false;
}
return true;
}
-
Jan 20, 2003, 15:41 #6
- Join Date
- Feb 2002
- Location
- NZ
- Posts
- 620
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thankyou
Bookmarks