Jump to content

[SOLVED] Help in preg_match


tabatsoy

Recommended Posts

here is my code:

 

             

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
$name = $_POST['name'];
if(preg_match("/^[a-zA-Z]$/",$name)){
	echo $name;
}
else	{
	echo 'invalid';
}
?>
<form action="testing.php" method="post">
  <p>
    <input name="name" type="text" id="name" />
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>

 

when i enter a 2 letter word it echoes invalid

please help.

Link to comment
https://forums.phpfreaks.com/topic/107632-solved-help-in-preg_match/
Share on other sites

^ means the begining of, and $ means the end, therefore the entire string is being compared.  (You might know that, but just making sure.)

 

Then you have [a-zA-Z].  Since there is no limit, 1 is assumed.

 

For example, to match 0 to 2....  /^[a-zA-Z]{0,2}$/

 

Or to match any amount it would just be /^[a-zA-Z]+$/

 

And more than one would be /^[a-zA-Z]+$/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.