Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Members Area 1

Status
Not open for further replies.

TrueJoker

Technical User
Jun 14, 2006
115
GB
Hi people,

I'm looking to allow certain characters when a person enters a name. At the moment letters upper and lower case are allowed but i'm looking to allow full stops and slashes as well but not sure how it is done!

Code:
if (!ereg("^[ZA-Za-z' -]{1,50}$",$company))
			{
				unset($_GET['do']);
				$message_new = "$company is not a valid company name. Please try again.";
				include("new_member.php");
				exit();
			}

here is the code i am using! im sure it is a fairly simple change but i ahve no idea at the moment! maybe i need more coffee lol
 
try this
Code:
<?php

$a = array ("jpadie", "justin.adie", "contains///", "contains][ illegal chars", "containsUPPERCASECHARS", "contains spaces");

echo "<table border=\"1\">";
foreach ($a as $address) {
	echo "<tr><td>$address</td><td>".matchme($address)."</td></tr>"; 
}
echo "</table>";
function matchme ($address) {
	$pattern = '/[^a-z\/\.]/i';
	if (preg_match($pattern, $address)){
		return "matched - address not valid";
	} else {
		return "not matched - address valid";
	}
}
?>
 
and to allow spaces change $pattern by adding a space just before the ]
Code:
$pattern = '/[^a-z\/\. ]/i';
 
thank you.

I think i understand that lol for some reason i just cant see the logic in defining what characters can be used such as upper and ower case slashes and full stops. Anyway, If i was to want to make it possible for the user to use numeric chars as well how would that be incorporated with

Code:
$pattern = '/[^a-z\/\. ]/i';
 
ok - here is the explanation

start with the pattern
Code:
$pattern = '/[^a-z\/\. ]/i';

Code:
/     the slash is used (by me at least) to denote the start and the end of the regex pattern itself.  modifiers go outside the pattern

[ ]    square brackets are used to denote a character class.  meaning anything in the class is ok.  
a-z  means anything from a to z is ok.  this is lower case only
/     as it is within the character class means a forward slash it ok.  because the forward slash is a special character we have to escape it first with the backslash
.     same as forward slash but for dots. the dot is a (very) special character in regex so also must be escaped.
     adding a space inside the char class allows spaces to be used too.
^  by putting a caret in front of the characters within the class this causes a negation.  so if any character is in the search string that is NOT in the char class, we will get a match and you will know that the name is invalid.  This is the essence of the logic: you are looking for illegal chars and not to make sure that every char is legal (if you see what i mean)
i   the modifier i makes the search strategy case insensitive.

to allow any word character including digits you can simplify the pattern as follows

[code]
$pattern = '/[^\w\/\. ]/';
the \w is a special shortcut that allows any word character (i.e. any letter or any digit). we could negate this by using a capital W (\W) but we would still have to negate the remaining chars so that would not really be a short cut.

 
Wow thanx =D that has actually made it pretty clear and i've saved it into a document for future reference lol thank you very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top