Jump to content

Archived

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

Terfanda

Validations

Recommended Posts

HI All

i have written this function to create a form


function CreateForm()
{

print("<form action=\"Petition.php\" method=post>\n");
print("First Name :<input type=text name=\"fname\" size=24 maxlength=32>");
print(" Last Name :<input type=text name=\"lname\" size=24 maxlength=32><br>\n");
print(" Email :<input type=text name=\"mailaddress\" size=30 maxlength=60><br>\n");
print("<input type=hidden name=\"BeenSubmitted\" value=\"TRUE\"><br>\n");
print("<input type=submit name=\"submit\" value=\"Submit\"></form>\n");
}


I need to know how can validate : fname, lname (check if they are empty or filled with numbers)
and check if the email entered is in the correct form

Thank you in advance

Share this post


Link to post
Share on other sites
I just installed AndyB's captcha script, which includes what you need and an anti-robot security feature. I'd try it first:

[a href=\"http://www.digitalmidget.com/php_noob/captcha.php\" target=\"_blank\"]http://www.digitalmidget.com/php_noob/captcha.php[/a]


just to check if a field is empty, you can do a few ways. If using $_POST, you can just check for the $_POST variable (if it's empty, a browser typically won't send it), or you could strlen the variable, too.
[code]if(!$_POST['fname']) $errorvar.="no first name\n";
if(!$_POST['lname']) $errorvar.="no last name\n";
if(!$_POST['email']) $errorvar.="no email address\n";[/code]

checking email address is a bit more complex... Here's a snipit from AndyB's script:
[code]        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $err.= $email. " is not a valid email address.<br/>";
        }
[/code]

hope that helps.

Share this post


Link to post
Share on other sites

×

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.