PDF='MAIN' { ATTRIBUTES='*' { PLATFORM='php.txt'; REPTYPE='1'; ATTACHED='0'; PROJECT='36'; OBJECTNAME='validate_email.php'; LIBLOBJ='*FILES'; TITLE='Email Validation'; WEBPATH='http://esdi.excelsystems.com/wsexmpphp/'; TARGETPATH='/www/zendcore/htdocs/examples/'; FTPSITE='examples'; SET='149'; VERSION='4.00'; CRTBNUM='Revision 1268'; CRTTEMPLT='C:\\Program Files\\WebSmart6.51 Client\\WebSmart\\Temp\\PHP-A Simple Page.tpl'; SAVBNUM='Revision 1811'; LASTLOC='1'; VHTML='N'; LASTSAVE='12/13/2007 10:15:49'; OBJLIBRARY='WEBTEST'; SOURCELIBR='WEBTEST'; SOURCEFILE='QRPGLESRC'; } FTPREFINFO='*' { URL='192.168.0.100'; INITIALDIR='/www/zendcore/htdocs/examples/'; FTPPORT='21'; PASSIVEMODE='1'; WEBPATH='http://esdi.excelsystems.com/wsexmpphp/'; } PANELS='*' { PANEL='MainSeg' { DESC='Main Segment'; DETAILS=' Email Validation PHP

Example 140: Email Address Validation (PHP)

Description:   This example will demonstrate how to validate a properly constructed email address submitted from a form in a PHP program.
-This example uses regular expressions to do the validation.

Enter email Address:
example: excel@excelsystems.com

$msg
  1. Start with a "PHP Simple Page" template.


  2. Add the following HTML code after <div id="contents"> on the \'MainSeg\' segment:
           <form name="val_email" action="validate_email.php" method="POST">
             <input type="hidden" name="task" value="val">
              <table>
                <tr>
                 <td>Enter email Address:<input type="text" name="email" size="30">
                 <input type="submit" value="Validate Email Address"></td>
                </tr>
               </table> 
              </form>
              
    <?php echo $msg; ?>
  3. In the PHP- Source Code create a global variable $msg.
    -This will be the message telling your users whether or not they entered a valid email address.

                global $msg;
                          


  4. In the PHP Source code, substitute this code:
       if ($pf_task == \'default\')
    	   generic();
    	                    

    With this code:
       switch($pf_task)
       {
    	  case \'default\':
    	  generic();
          break;
    	
    	  case \'val\':
    	  validate();
    	  break;
        }
                            

  5. Create the validate function.
    Add this code under the generic function:
    function validate()
     {
      // Make all global variables available here
      foreach($GLOBALS as $arraykey=>$arrayvalue) 
    	{
    	 if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\')
    	 global $$arraykey;
    	}
    
      $email = $_POST["email"];
      if(!eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$", $email))
       	$msg = "Please enter a valid email address";
      else
        $msg = "You have entered a valid email address";
        
      wrtseg(\'MainSeg\');
    	
     }
                          
  6. Compile and run your progam.

Program Definition:   validate_email.pdw

Rate This Example

Did this knowledge base article help you to achieve your goal?  Yes  No  Don\'t Know

Enter additional comments below.   If you want to hear back from us, include your contact information.

'; } } PREVIEWS='*' { DEFAULT='MainSeg'; PREVIEW='MainSeg' { SEG='MainSeg'; } } ACTIONS=' // Program Name: validate_email.php // Program Title: Email Validation // Created by: SPOT // Template family: Idaho // Template name: Simple Page // Purpose: // Program Modifications: require(\'/esdi/websmart/v6.6/include/xl_functions001.php\'); global $wsnum, $msg, $email; // This code is for the Rate this page section if (isset($_REQUEST[\'wsnum\'])) { $wsnum = $_REQUEST[\'wsnum\']; } // Check the cookie if(isset($_COOKIE[\'wsnumbc\'])) { $wsnum = $_COOKIE[\'wsnumbc\']; } else { setcookie(\'wsnumbc\', $wsnum); } // Retrieve the task (default to "default") switch($pf_task) { case \'default\': generic(); break; case \'val\': validate(); break; } function generic() { wrtseg(\'MainSeg\'); } function validate() { // Make all global variables available here foreach($GLOBALS as $arraykey=>$arrayvalue) { if($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\') global $$arraykey; } $email = $_POST["email"]; if(!eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$", $email)) $msg = "\\"Please verify that your email address is correctly formatted.\\""; else $msg = "You have entered a valid email address."; wrtseg(\'MainSeg\'); }'; }