Forum Moderators: coopster

Message Too Old, No Replies

Validating Forms By Characters

         

Programmers

8:57 am on Jul 13, 2006 (gmt 0)

10+ Year Member



I have been validating the forms all around my website recently as I have been learning PHP and one thing I don't know is how to propperly validate to specific things.

For example, I've set up my forms so they can't be submitted with certain or no data. However, how do I set it so they can't be submitted if they contain certain characters or vise versa.

For example:

Say the user submitted a URL instead of an email, then there would be an error because there was no '@' character used.

Sekka

9:00 am on Jul 13, 2006 (gmt 0)

10+ Year Member



Use strpos () to determine if the @ symbol is present.

e.g.

$pos = strpos("myemail@mydomain.co.uk", "@");

if ($pos === false) {
// No @ symbol exists
} else if ($pos > 1) {
// Too many @ symbols are present
} else {
// Valid
}

Programmers

9:23 am on Jul 13, 2006 (gmt 0)

10+ Year Member



Thanks for your reply, but it never seemed to work, I got an error no matter what I tried, and the error was always on line five. Here's my code.

<?

$email = trim($_POST['email']);
$myemail = "***@***.co.uk";
$pos = strpos($email "@");

if($pos === false)
{ echo "<h1>Error</h1><br>..."; }

elseif($pos > 1)
{ echo "<h1>Error</h1><br>..."; }

elseif($email == "your-email@domain.com")
{ echo "<h1>Error</h1><br>..."; }

elseif($email == "")
{ echo "<h1>Error</h1><br>..."; }

else
{
mail($myemail, "Mailing List", "Email: $email");
echo "<h1>Thank You</h1><br>...";
}
?>

Here are all the alternatives that I tried:

$pos = ($email "@");

$pos = ("$email" "@");

$pos = $email "@";

$pos = "$email" "@");

$pos = strpos($email "@");

$pos = strpos("$email" "@");

$pos = ($email, "@");

$pos = ("$email", "@");

$pos = $email, "@";

$pos = "$email", "@");

$pos = strpos($email, "@");

$pos = strpos("$email", "@");

Sekka

9:54 am on Jul 13, 2006 (gmt 0)

10+ Year Member



First of all,

$pos = strpos($email "@");

There is no , between $email and "@", it needs to be,

$pos = strpos($email, "@");

It should work. :/

Programmers

10:15 am on Jul 13, 2006 (gmt 0)

10+ Year Member



Ace stuff it works now if they don't enter an @ character. However, if they enter too many it still submits.

Sekka

10:50 am on Jul 13, 2006 (gmt 0)

10+ Year Member



My head is not screwed on today. :(

This is better,

$email = "myemail@mydomain.com";

$result = substr_count ($email, "@");

if ($result == 1) {
// Valid
} else {
// Not valid
}

dreamcatcher

1:38 pm on Jul 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or you can do:

if (strstr($email,'@'))
{
//Invalid
}

dc

justgowithit

2:34 pm on Jul 13, 2006 (gmt 0)

10+ Year Member



function valid_email($address){
if (ereg('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address))
return true;
else
return false;
}

===================================

if(!valid_email($email)){
echo 'Please Enter a valid email address';
}else{
//-> proceed with form validation or submit
}

le_gber

2:42 pm on Jul 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



or:

$isEmail = '/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i';

if(preg_match($isEmail, $email)){

it's an email

}else{

it's not

}

regular expression can be very helpful and I had a hard time getting my head round it at the very begining. I'll try to explain the above $isEmail (reg expression used by C.Shiflett in his book):

/^: starts regular expression
[^@\s<&>]: you cannot have (^) a @ or a space (\s) or < or & or > in the first part of the email [ ]
+@: you have to have a @
([-a-z0-9]: then a combination of letters (lowercases) and/or numbers with possibly hyphens (the first -)
+\.: then a . (the . has to be escaped with \
+[a-z]{2,} and finally at least 2 lowecase letters
$/i: ends regular expression

[edited by: le_gber at 3:06 pm (utc) on July 13, 2006]

justgowithit

2:53 pm on Jul 13, 2006 (gmt 0)

10+ Year Member



Thanks le_gber, I like the variable reference to the regexp to eliminate the need for the function. I'd assume this approach uses less resources as well.

Sekka

2:59 pm on Jul 13, 2006 (gmt 0)

10+ Year Member



Thanks le_gber

I've been trying to get my head around regular expression and this helps a lot.

:)

Programmers

3:50 pm on Jul 13, 2006 (gmt 0)

10+ Year Member



There are some excellent things there, and now my forms work perfect! Thanks everyone.

I'd like to understand how they work though, so could someone just briefly explain how the following part works, please:

$variable = strstring($email, "@")

justgowithit

4:47 pm on Jul 13, 2006 (gmt 0)

10+ Year Member



strstr() [us3.php.net] is a pre-defined function that looks for the first occurrence of a string (in this case the "@" character).

This line of code would not be needed if you use the solution that myself or le_gber posted.

Programmers

4:04 pm on Jul 14, 2006 (gmt 0)

10+ Year Member



Thank you. =)