Forum Moderators: coopster

Message Too Old, No Replies

Form Validation

Explanation

         

wfernley

1:45 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi everyone.

Well I am working on Form Validation with PHP. I have been looking at many tutorials and all of them have given the code to validate but havent explained how it works.

Basically I have this script:

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

It validates an email address. How does it work? How can I manipulate it to validate other inputs, such as address, phone numbers or any kind of input?

Thanks for your help :)

Wes

ScottYardley

2:47 pm on Oct 6, 2004 (gmt 0)

10+ Year Member



You need to study regular expressions
Here's a basic understanding of what your code is doing:

^
means to start at the beginning of the string.

[a-zA-Z0-9_\.\-]
looking for a-z or A-Z or 0-9 or - or .
+

@
looking for the the "@"

[a-zA-Z0-9\-]
looking for a-z or A-Z or 0-9 or -

+

\.
looking for a period

[a-zA-Z0-9\-\.]
looking for a-z or A-Z or 0-9 or - or .

+
$

wfernley

4:34 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great thanks for the reply. I will just have to figure out how to seperate it and adapt it to the other inputs in the form.

Wes

Adrian2k4

1:38 am on Oct 7, 2004 (gmt 0)

10+ Year Member



What you need is a Regex tutorial.
Check out the following links:

First get an idea of what it's all about with the following tutorial:
[phpfreaks.com...]

Then look at this more advanced one:
[phpfreaks.com...]

After you read through those two, tested the examples etc. you shouldn't have too many problems validating your form entries.

best regards
adrian