tabatsoy Posted May 28, 2008 Share Posted May 28, 2008 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 More sharing options...
corbin Posted May 28, 2008 Share Posted May 28, 2008 ^ 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]+$/ Link to comment https://forums.phpfreaks.com/topic/107632-solved-help-in-preg_match/#findComment-551703 Share on other sites More sharing options...
thebadbad Posted May 28, 2008 Share Posted May 28, 2008 And, just so you know, ctype_alpha() will check for alphabetic input and return true or false. Will be faster than the equivalent preg_match. Link to comment https://forums.phpfreaks.com/topic/107632-solved-help-in-preg_match/#findComment-551729 Share on other sites More sharing options...
tabatsoy Posted May 28, 2008 Author Share Posted May 28, 2008 thanks man i forgot to put the plus sign at the end i love this site Link to comment https://forums.phpfreaks.com/topic/107632-solved-help-in-preg_match/#findComment-551739 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.