27089 total geeks with 3528 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
timsattemme
<ul><li><str ong><a href="http:/ /www.ywjx8.c om/">cheap wedding dresses</a>< /strong> </l i><li><stron g><a href="http:/ /www.ywjx8.c om/">Sexy Beach Wedding Dresses Sale</a></st rong> </li>< li><strong>< a href="http:/ /www.ywjx8.c om/">cheap wedding dresses</a>< /st
timsattemme
[b][url=http ://www.watch esiphone5.co m/]replica o<strong><a href="http:/ /www.watches iphone5.com/ ">replica omega watches</a>< /strong> <br > <strong><a href="http:/ /www.watches iphone5.com/ ">replica cartier watches</a>< /strong> <br >
timsattemme
<strong><a href="http:/ /www.watches iphone5.com/ ">cartier watches</a>< /strong> | <strong><a href="http:/ /www.watches iphone5.com/ ">rolex watches</a>< /strong> | <strong><a href="http:/ /www.watches iphone5.com/ ">omega watches</a>< /strong> <br > <title>Longi
timsattemme
[b][url=http ://www.copyr olexdaytona. com/]rolex new 2013[/url][/ b] [b][url=h ttp://www.co pyrolexdayto na.com/]role x basel[/url][ /b] [b][url=http ://www.copyr olexdaytona. com/]rolex Y<br><strong ><a href="http:/ /www.copyrol exdaytona.co m/">rolex new 2013</a></st
timsattemme
<br><strong> <a href="http:/ /www.copyrol exdaytona.co m/">rolex new 2013</a></st rong> <stron g><a href="http:/ /www.copyrol exdaytona.co m/">rolex basel</a></s trong> <br>< strong><a href="http:/ /www.copyrol exdaytona.co m/">rolex Yacht-Master II</a></stro ng> <br><b

Donate
Donate and help us fund new challenges
Donate!
Due Date: Oct 31
October Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
SKYPE has the HOTS
for my NAKED WIFE
Time to test your
sarcasm detectors:
It"s the UN"s
global comms
shakeup
extravaganza!
Wanna see how
Russia et al hope
to shape the
internet? ITU opens
up (a little more)
t
Google CEO Larry
Page gives Sundar
Pichai keys to the
kingdom
Cheapo telcos fined
for their cheapo
security: Financial
records on 305,000
people spill
LG taps TSMC to
bake its first-ever
mobile chip
FCC: You"ll have to
wait even longer
for faster wireless
broadband
Breaking news:
Google exec in
terrifying SKY
PLUNGE DRAMA
Revealed: The
amazing magical
innovation in the
iPad Mini 3 ? a
lick of paint
Silicon Valley
scrooges paid staff
$1.21 an hour in a
122-hour week
Slashdot
Peter Kuran:Visual
Effects Artist and
Atomic Bomb
Archivist
OwnCloud Dev
Requests Removal
From Ubuntu Repos
Over Security Holes
Microsoft Now Makes
Money From Surface
Line, Q1 Sales
Reach Almost $1
Billion
Days After
Shooting, Canada
Proposes New
Restrictions On and
Offline
AT&T Locks
Apple SIM Cards On
New iPads
Passwords: Too Much
and Not Enough
Verizon Injects
Unique IDs Into
HTTP Traffic
Secretive Funding
Fuels Ongoing Net
Neutrality
Astroturfing
Controversy
A Low Cost, Open
Source Geiger
Counter (Video)
Computer Scientist
Parachutes From
135,908 Feet,
Breaking Record
Article viewer

PHP Form Field Data Validation Example/Tutorial



Written by:dimport
Published by:SAJChurchey
Published on:2003-06-21 07:19:46
Topic:PHP
Search OSI about PHP.More articles by dimport.
 viewed 45547 times send this article printer friendly

Digg this!
    Rate this article :
This is a simple script to illustrate the of a function, field_validator(), which I modified from an original piece of code submitted on the devshed forums.

The field_validator() function is an extremely useful function for validating simple data that is returned to your PHP script from an HTML form. By using the function you can validate that the data a user enters on a form is of the correct type (i.e. email, string, digit, number, alpha, alphanumeric) and that it is of the
correct length (i.e. > min length).

The function is as follows:

function field_validator($field_descr, $field_data, $field_type, $min_length="", $max_length="", $field_required=1) {
    # array for storing error messages
    global $messages;
    
    # first, if no data and field is not required, just return now:
    if(!$field_data && !$field_required){ return; }
     # initialize a flag variable - used to flag whether data is valid or not
    $field_ok=false;
     # this is the regexp for email validation:
    $email_regexp="^([a-zA-Z0-9_-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|"
    $email_regexp.="(([a-zA-Z0-9-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$";
     # a hash array of "types of data" pointing to "regexps" used to validate the data:
    $data_types=array(
        "email"=>$email_regexp,
        "digit"=>"^[0-9]$",
        "number"=>"^[0-9]+$",
        "alpha"=>"^[a-zA-Z]+$",
        "alpha_space"=>"^[a-zA-Z ]+$",
        "alphanumeric"=>"^[a-zA-Z0-9]+$",
        "alphanumeric_space"=>"^[a-zA-Z0-9 ]+$",
        "string"=>""
    );
    
    # check for required fields
    if ($field_required && empty($field_data)) {
        $messages[] = "$field_descr is a required field.";
        return;
    }
    
    # if field type is a string, no need to check regexp:
    if ($field_type == "string") {
        $field_ok = true;
    } else {
        # Check the field data against the regexp pattern:
        $field_ok = ereg($data_types[$field_type], $field_data);
    }
    
    # if field data is bad, add message:
    if (!$field_ok) {
        $messages[] = "Please enter a valid $field_descr.";
        return;
    }
    
    # field data min length checking:
    if ($field_ok && $min_length) {
        if (strlen($field_data) $max_length) {
            $messages[] = "$field_descr is invalid, it should be less than $max_length characters.";
            return;
        }
    }
}


This article was originally written by munk

Did you like this article? There are hundreds more.

Comments:
Anonymous
2008-03-11 17:53:17
Looks like the script is missing the ; after $email_regexp="^([a-zA-Z0-9_-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|"

Also, add a > in the max length comparison
if (strlen($field_data) > $max_length) {
Anonymous
2009-01-01 18:09:37
okay
Anonymous
2009-07-13 16:24:53
Great tutorial! I have added this to my new famous logos blog. You can see a list of famous company logo designs there too,
Anonymous
2010-01-20 07:37:58
12115
Anonymous
2010-02-12 19:41:41
fffff
Anonymous
2010-02-14 04:09:48
Excellent. Thanks a ton.
Anonymous
2010-03-12 19:44:40
When do you invoke this function?
Anonymous
2010-03-17 05:46:54
the code is good. For someone that doesn't know much about how to implement this into a form; they'll have trouble though.

- James
<a href="http://www.fendza.com">Employee Scheduling Software by Fendza.com</a>
Anonymous
2010-03-17 05:50:26
Forgot to add; may want to add in there a quick how-to on how to not only implement, but also convert it to fit different forms by where to input the id tags.

James
Employee scheduling software by Fendza
Anonymous
2011-02-02 20:38:47
Daily Deals
Anonymous
2011-06-07 16:53:03
nu stiu de astea!!
ObatAsamUrat
2011-06-16 07:15:02
Exceptional blog site in this article! Moreover your internet internet site tons way up rapidly! Precisely what number do you think you're the application of? Am i allowed to are taking your internet marketer url on your number? I need our web page crammed as speedily while your own house : )
obat asam urat
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
elasolova
My PHP Projects on Sat 26th Sep 10am
I have been developing PHP applications for almost a year now. I have developed three projects. One is a simple trivia game. The other is a question-answer based community at http://www.javaist.com/quans . The last one is a programming challenge site just
countll
Blog entry for Thu 25th Oct 7am on Thu 25th Oct 7am
soo nu on this wicked world of NET. just decided to dive in today..hope friend aroun here can help

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Test of experience (hopefully) by AcidIce

Things you're only likely to know if you've actually written a lot of PHP before :)


     
Your Ad Here
 
Copyright Open Source Institute, 2006