Help - Search - Members - Calendar
Full Version: Ereg For Letters Only
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
loveandasandwich
Hello,

I was hoping someone could tell me how to do an ereg() for just letters, no numbers. The reason is for name validation. Like, if someone puts "IAmJoe763456" into the name field, it will say that's not a valid name because, well, it's not.

Here's my code so far. The area I need changed is the "EREG CRAP GOES HERE". Lol.
CODE
<?php
if(isset($_POST['submit'])){
        $to = "artizhay@gmail.com";
        $from = $_POST['email'];
        $subject = "Love And A Sandwich Form Submission";
        $msg = $_POST['msg'];
        $name = $_POST['name'];
        $captcha = $_POST['captcha'];

if (empty($name)) {
echo "Please enter your name. <br><br>";
} elseif (! ereg('EREG CRAP GOES HERE', $name)) {
echo "Please enter a valid name. <br><br>";
} elseif (empty($from)) {
echo "Please enter your e-mail address. <br><br>";
} elseif (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $from)) {
echo "Please enter a valid e-mail address. <br><br>";
} elseif (empty($msg)) {
echo "Please enter your message. <br><br>";
} elseif ($captcha != "human") {
echo "Please enter the word \"human\" where designated. <br><br";
} else {

if (isset ($_FILES['att'])){
        $fileatt = $_FILES['att']['tmp_name'];
        $fileatttype = $_FILES['att']['type'];
        $fileattname = $_FILES['att']['name'];
        $fileattsize = $_FILES['att']['size'];
}
    
        $headers = "From: loveandasandwich@gmail.com";

if ($fileattsize != 0) {
        $file = fopen( $fileatt, 'rb' );
        $data = fread( $file, filesize( $fileatt ) );
        fclose( $file );

        $semi_rand = md5( time() );
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
        $headers .= "\nMIME-Version: 1.0\n" .
                    "Content-Type: multipart/mixed;\n" .
                    " boundary=\"{$mime_boundary}\"";

        $message = "The sender's e-mail is $from and his or her name is $name\n\n" .
                   "His or her message is as follows:\n" .
                   "$msg";
    
        $message .= "This is a multi-part message in MIME format.\n\n" .
                "--{$mime_boundary}\n" .
                "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
                "Content-Transfer-Encoding: 7bit\n\n" .
                $message . "\n\n";
    
        $data = chunk_split( base64_encode( $data ) );
                
        $message .= "--{$mime_boundary}\n" .
                 "Content-Type: {$fileatttype};\n" .
                 " name=\"{$fileattname}\"\n" .
                 "Content-Disposition: attachment;\n" .
                 " filename=\"{$fileattname}\"\n" .
                 "Content-Transfer-Encoding: base64\n\n" .
                 $data . "\n\n" .
                 "--{$mime_boundary}--\n";

        if( mail( $to, $subject, $message, $headers ) ) {
        
            echo "<p>The email was sent.</p>";
        
        }
        else {
        
            echo "<p>There was an error sending the mail.</p>";
        
        }
   } else {
        $body = "The sender's e-mail is $from and his or her name is $name.\n\n" .
                "His or her message is as follows:\n" .
                "$msg";
        if( mail( $to, $subject, $body, $headers ) ) {
        
            echo "<p>The email was sent.</p>";
        
        }
        else {
        
            echo "<p>There was an error sending the mail.</p>";
        
        }
     }
   }
}
        
?>
<form action="<?php echo $_server['php-self'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
        Name: <input name="name" id="name" type="text" value="<?php print $name; ?>" /><br>
        E-mail: <input name="email" id="email" type="text" value="<?php print $from; ?>" /><br>
        Message:<br>
        <textarea name="msg" id="msg" cols="50" rows="10" wrap="hard" /><?php print $msg; ?></textarea><br>
        Attachment: <input name="att" id="att" size="30" type="file" class="fileUpload" /><br>
        Enter the word "human":<br>
    <input name="captcha" id="captcha" type="text" value="<?php print $captcha; ?>" /><br>
        <button name="submit" type="submit" class="submitButton">Send E-mail!</button><br>
</form>
Alex
Uh, that's pretty basic regex, ^[a-zA-Z]+$ will do what you intend.

^ denotes the start of the line
[a-zA-Z]+ means as many characters in the ranges a-z or A-Z as needed
$ denotes the end of the line

Though, personally for a name I would also include spaces, so ^[ a-zA-Z]+$ might be a better choice.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.