Page 1 of 1

Help in preg_match

Posted: Wed May 28, 2008 10:15 am
by tabatsoy
here is my code:

Code: Select all

<!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.

Re: Help in preg_match

Posted: Wed May 28, 2008 10:24 am
by onion2k
/^[a-zA-Z]$/ will only match a single letter. Use /^[a-zA-Z]{1,2}$/ to match 1 or 2 characters.. or /^[a-zA-Z]+$/ to match any number of letters.

EDIT: This form field is called 'name' .. if it's a persons name you need to let the user add a lot more than just letters. Accented letters, apostrophes, hyphens, spaces and even numbers can appear in a name.

Re: Help in preg_match

Posted: Wed May 28, 2008 10:45 am
by tabatsoy
thanks man.

this topic is solved

i love this site