Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 02-16-2007, 01:25 PM Form Validation
Red_X_'s Avatar
Extreme Talker

Posts: 153
Location: Houston
Trades: 0
Ok im creating a very simple form, for users to register there information on my site. However, I know how to make the front end, and do 'some' of the back end. But I want to know how to make the form validate using global variables.

For the first and last name this is what I want done:
  • cannot be empty string
  • cannot contain only empty space
  • cannot contain more than 20 characters
  • cannot contain any numerical characters
  • can contain hypen and empty space must start and end with letter
And for my street address, all I want is:
  • Cannot be empty string
  • Cannot have only empty space(s)
And for the zip code:
  • Must have only a total of 5 numerical characters
And for a e-mail:
1. Email must contain @



The label names are as followed:

First Name: f_name
Last Name: l_name
Zip Code: zip
Street Address: s_address
Email: email

Like I said above, wouldn't the easiest way go about just using the "Globals" variable? Or would there be a simpilar way?

HTML CODE:
HTML Code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>User</title>
</head>

<body><form method='post' action="verifty.php">
<table width="377" border="1">
  <tr>
    <td width="117">Customer Data </td>
    <td width="244"></td>
  </tr>
  <tr>
    <td>First Name: </td>
    <td><input name='f_name' type="text" size='15'/></td>
  </tr>
  <tr>
    <td>Last Name: </td>
    <td><input name='l_name' type="text" size='15'/></td>
  </tr>
  <tr>
    <td>Street:</td>
    <td><input name='street' type="text" size='40'/></td>
  </tr>
  <tr>
    <td>City:</td>
    <td><input name='city' type="text" size='15'/></td>
  </tr>
  <tr>
    <td>Zip:</td>
    <td><input name='zip' type="text" size='15'/></td>
  </tr>
  <tr>
    <td>State:</td>
    <td><select name="state" id="state" onChange="mySubmit();">
             <OPTION value="0">-</option>
             <OPTION value=AK>AK</option>
             <OPTION value=AL>AL</option>
             <OPTION value=AR>AR</option>
             <OPTION value=AZ>AZ</option>
             <OPTION value=CA>CA</option>
             <OPTION value=CO>CO</option>
             <OPTION value=CT>CT</option>
             <OPTION value=DC>DC</option>
             <OPTION value=DE>DE</option>
             <OPTION value=FL>FL</option>
             <OPTION value=GA>GA</option>
             <OPTION value=HI>HI</option>
             <OPTION value=IA>IA</option>
             <OPTION value=ID>ID</option>
             <OPTION value=IL>IL</option>
             <OPTION value=IN>IN</option>
             <OPTION value=KS>KS</option>
             <OPTION value=KY>KY</option>
             <OPTION value=LA>LA</option>
             <OPTION value=MA>MA</option>
             <OPTION value=MD>MD</option>
             <OPTION value=ME>ME</option>
             <OPTION value=MI>MI</option>
             <OPTION value=MN>MN</option>
             <OPTION value=MO>MO</option>
             <OPTION value=MS>MS</option>
             <OPTION value=MT>MT</option>
             <OPTION value=NC>NC</option>
             <OPTION value=ND>ND</option>
             <OPTION value=NE>NE</option>
             <OPTION value=NH>NH</option>
             <OPTION value=NJ>NJ</option>
             <OPTION value=NM>NM</option>
             <OPTION value=NV>NV</option>
             <OPTION value=NY>NY</option>
             <OPTION value=OH>OH</option>
             <OPTION value=OK>OK</option>
             <OPTION value=OR>OR</option>
             <OPTION value=PA>PA</option>
             <OPTION value=RI>RI</option>
             <OPTION value=SC>SC</option>
             <OPTION value=SD>SD</option>
             <OPTION value=TN>TN</option>
             <OPTION value=TX>TX</option>
             <OPTION value=UT>UT</option>
             <OPTION value=VA>VA</option>
             <OPTION value=VT>VT</option>
             <OPTION value=WA>WA</option>
             <OPTION value=WI>WI</option>
             <OPTION value=WV>WV</option>
             <OPTION value=WY>WY</option>
           </SELECT></td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input name='email' type="text" size='30'/></td>
  </tr>
  <tr>
      <td>Submit:</td>
    <td><input type='submit' name='submit' /></td>
  </tr>
</table>
</form>
</body>


PHP CODE(this is all i've got right now(and I'm confused after this)):
PHP Code:
<?php
// Collect User Information, and store it in variables
    
$f_name $_POST['f_name'];
    
$l_name $_POST['l_name'];
    
$street $_POST['street'];
    
$zip $_POST['zip'];
    
$city $_POST['city'];
    
$state $_POST['state'];
    
$email $_POST['email'];

    
user();
    
    function 
user();
        if(
validation()) {
                echo 
"Name: ".$GLOBALS['f_name']
?>
__________________
"Good News Everyone, by reading this your hearing my voice."

Last edited by Red_X_; 02-16-2007 at 01:38 PM..
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
 
Register now for full access!
Old 02-16-2007, 02:48 PM Re: Form Validation
memberpro's Avatar
Super Talker

Posts: 143
Trades: 0
I wouldn't use PHP to validate the form... I would use a simple Javascript to do this. Google "javascript form validation" and you will find plenty of tutorials on you can create a very quick, simple, validating program.

If you are dead set on using PHP then you are going to need a very large IF statement to do what you are looking for.. i.e.

Code:
If ($email =="" || $fname =="" || $lname="' || on and on) {
 
Submit form code; }
 
else  {
 
Go back to the form and enter the right info; }
Best of Luck
__________________

Please login or register to view this content. Registration is FREE
- step-by-step learn how to design, create and install your own website in hours...not days.
Please login or register to view this content. Registration is FREE
was never so easy.

Last edited by memberpro; 02-16-2007 at 02:54 PM..
memberpro is offline
Reply With Quote
View Public Profile
 
Old 02-16-2007, 03:07 PM Re: Form Validation
Red_X_'s Avatar
Extreme Talker

Posts: 153
Location: Houston
Trades: 0
Quote:
Originally Posted by memberpro View Post
I wouldn't use PHP to validate the form... I would use a simple Javascript to do this. Google "javascript form validation" and you will find plenty of tutorials on you can create a very quick, simple, validating program.

If you are dead set on using PHP then you are going to need a very large IF statement to do what you are looking for.. i.e.

Code:
If ($email =="" || $fname =="" || $lname="' || on and on) {
 
Submit form code; }
 
else  {
 
Go back to the form and enter the right info; }
Best of Luck

I would have done that, but I want to use other PHP functions. If you read above, theres gotta be a way to do those?
__________________
"Good News Everyone, by reading this your hearing my voice."
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
Old 02-16-2007, 04:36 PM Re: Form Validation
memberpro's Avatar
Super Talker

Posts: 143
Trades: 0
The function to count the number of characters in a string is strlen

The ansi character code for 'space' is 32 so check your field against char(32)

ansi characters for 0-9 is 48-57 -> same as above

To get each character of a string use the brackets for each letter (i.e. for the first letter in a string use $mystring[0], second letter $mystring[1], etc.)


That should be enough information for you to write you own script.
__________________

Please login or register to view this content. Registration is FREE
- step-by-step learn how to design, create and install your own website in hours...not days.
Please login or register to view this content. Registration is FREE
was never so easy.
memberpro is offline
Reply With Quote
View Public Profile
 
Old 02-16-2007, 04:52 PM Re: Form Validation
Mooofasa's Avatar
Defies a Status

Posts: 1,611
Name: Michael (mik) Land
Location: England
Trades: 0
Well, you wanted some pretty basic things. I've made a small script that will handle the first name and last name of your request. It's all heavily commented, so if you don't get it just ask.

PHP Code:
<?php
    
// is first name or last name empty or just spaces?
        
if (empty($f_name) || empty($l_name) || $f_name == ' ' || $l_name == ' ') {
            echo 
'error';
        } 
// end if
        
        
else {
            
// first name longer than 20 characters (use strlen)?
                
if (strlen($f_name) > '20') {
                     
// kill
                        
die('First name too long (must be equal to or less than 20)');
                } 
// end if
            
            // last name longer than 20 characters?
                
elseif (strlen($l_name) > '20') {
                     
// kill
                        
die('Last name too long (must be equal to or less than 20)');
                } 
// end elseif
            
                
else {
                    
// numeric values (use ctype_)?
                        
if (ctype_digit($f_name) || ctype_digit($l_name)) {
                            die(
'Names cannot contain numeric values');
                        } 
// end if
                        
                        
else {
                            
// DO WHATEVER YOU WANT NOW
                        
// end else
                
// end else
        
// end else
?>

Last edited by Mooofasa; 02-16-2007 at 04:53 PM..
Mooofasa is offline
Reply With Quote
View Public Profile Visit Mooofasa's homepage!
 
Old 02-16-2007, 09:43 PM Re: Form Validation
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
This is where regular expressions are handy as they match string patterns. A handy tutorial can be found at: http://www.regular-expressions.info/

PHP Code:
<?php
  
  
// Evaluate first/last name:
  
if (preg_match('/^[a-z]{1}[a-z- ]{0,18}[a-z]{1}$/i'trim(stripslashes($_POST['f_name']) . ' ' stripslashes($_POST['l_name'])))) {}  // Evaluates to true if correct
  
  // Evaluate street address:
  
if (trim(stripslashes($_POST['s_address'])) != '') {}  // Evaluates to true if not empty
  
  // Evaluate zip code:
  
if (preg_match('/^[0-9]{5}$/'trim(stripslashes($_POST['zip'])))) {}  // Evaluates to true if 5 digits
  
  // Evaluate email address:
  
if (preg_match('/^[a-z0-9-_]+@[a-z0-9-_]+\.[a-z]{3,4}$/i'trim(stripslashes($_POST['email'])))) {}  // Evaluates to true if correct
  
?>
__________________

<mgraphic /> - I don't have a solution but I admire the problem.

Last edited by mgraphic; 02-16-2007 at 09:46 PM.. Reason: incomplete example
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-16-2007, 10:36 PM Re: Form Validation
memberpro's Avatar
Super Talker

Posts: 143
Trades: 0
Thanks for the link to the tutorial on expressions. I have not taken the time to learn how to use expressions well.

Great Resource.
__________________

Please login or register to view this content. Registration is FREE
- step-by-step learn how to design, create and install your own website in hours...not days.
Please login or register to view this content. Registration is FREE
was never so easy.
memberpro is offline
Reply With Quote
View Public Profile
 
Old 02-17-2007, 03:10 AM Re: Form Validation
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Quote:
Originally Posted by memberpro View Post
I wouldn't use PHP to validate the form... I would use a simple Javascript to do this. Google "javascript form validation" and you will find plenty of tutorials on you can create a very quick, simple, validating program.
Yes, and if the user has disabled JS in his browser you will get crap in your database or whatever.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 02-17-2007, 08:28 PM Re: Form Validation
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
I would use both client and server side checking, client side to help ease unnecessary server operations. It's the first I've heard of javascript being disabled you'd end up with crap in your database, but I guess when you use javascript as your only means of validation, which you should never rely just on.

Global variables don't exist from posted variables unless global register is on, that way information posted automatically become set. Using $GLOBALS['f_name']; suggests that $f_name; was created in the global scope. Nor are global variables the way to go about this, just the usual validation routine checking.

Validation should not be ended until it gets to the part where the action of the script would take place (e.g. stored in database or emailed, etc), in psuedo "if errors then return". This way you can notify the user of all errors, instead of asking them to fix 1 problem at a time. Doing that just means setting a status flag to say errors exist, as well as having a variable to contain all error messages unless you come up with a better error handling means.

Although mgraphic means well, Names can consist of more characters than what he allows for, I would rather hope someone knows their own name and just check for malicious content as well as making sure they meet the requirements.

His email pattern could do better too, even though I wrote the complete RFC821 specification as a pattern for validating emails, I had to write a more realistic pattern for what is currently being allowed by email companies. If I can track it down I'll post it, but I know I wrote a very simplistic one on another forum which started my journey into email validation techniques.

The problems with his email pattern reading from the start of it.

It doesn't conform to what email companies are allowing, you can't use numbers and other characters as the start of the email, when most companies only allow a letter first. There's no limit to it and doesn't allow for periods.

The domain name part again doesn't conform to what domains are allowed.

Next up, he's limited what the end could be, we can't use .tk because it's only 2 characters, not only that, we can't use .co.uk, .com.au, etc because he doesn't allow for a second period, also being limited 4 characters, I know there's some endings that are 5 characters long, in NZ .maori.nz and there could possibly be longer ones existing.

To easily bypass his email pattern, just do -@-.aaa, but you'd probably want to do email confirmation to validate as well, but at least fix it up enough so that all correct emails can still get through and you don't waste your time trying to send emails that really don't exist because your validator didn't stop it at the start.

If I'm allowed to, I'll drop my validation class (js and php) in here too, which just works off basic types that you can configure the formatting for, to match what you like, e.g. telephone numbers in the format of (+##) #-###-#### etc.

Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Reply     « Reply to Form Validation
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31214 seconds with 12 queries