član
Certified
Datum učlanjenja: 26.07.2008
Poruke: 76
Hvala: 15
296 "Hvala" u 10 poruka
|
I, evo, zahvaljujuci nesebicnoj webartovoj pomoci, poboljsane verzije:
PHP kôd:
<?php class Validator { public function __construct($prefix) { foreach ($_POST as $key=>$value) { if(strpos($key,$prefix)!==false) { $_SESSION[$key]=$value; } } } public $errors = array( ); public $output = array( ); public function process( $vars ) { for ( $i = 0; $i < count( $vars ); $i++ ) { $title = $vars[ $i ][ 0 ]; $rules = $vars[ $i ][ 1 ]; $var = $vars[ $i ][ 2 ]; //check required field if ( strpos( $rules, 'req' ) !== false ) { if ( $this->required( $var ) ) { $this->errors[] = $title . " is required field!"; } } //only text is allowed if ( strpos( $rules, 'text' ) !== FALSE ) { if ( $this->only_text( $var ) ) { $this->errors[] = $title . ' field should contain only letters!'; } } //only numbers are allowed if ( strpos( $rules, 'num' ) !== FALSE ) { if ( $this->only_numbers( $var ) ) { $this->errors[] = $title . ' field should contain only numbers!'; } } //check minimal number of chars if ( strpos( $rules, 'min=' ) !== false ) { $num = $this->min( $var, $rules ); if ( is_numeric( $num ) ) { $this->errors[] = $title . " should have at least $num characters!"; } elseif ( $num === false ) { $this->errors[] = 'Invalid rule.'; } } //check max number of chars if ( strpos( $rules, 'max=' ) !== false ) { $num = $this->max( $var, $rules ); if ( is_numeric( $num ) ) { $this->errors[] = $title . " should have at most $num characters!"; } elseif ( $num === false ) { $this->errors[] = 'Invalid rule.'; } } //check email if ( strpos( $rules, 'valid_email' ) !== FALSE ) { if ( $this->valid_email( $var ) ) { $this->errors[] = $title . ' is not a valid email address!'; } } //check single checkbox if ( strpos( $rules, 'type:check_single' ) !== FALSE ) { if ( $this->single_checkbox( $var, $rules ) ) { $this->errors[] = $title . " wrong entry!"; } } //check checkbox group if ( strpos( $rules, 'type:check_multi' ) !== FALSE ) { if ( $this->multi_checkbox( $var, $rules ) ) { $this->errors[] = $title . " wrong entry!"; } } //check radio buttons group if ( strpos( $rules, 'type:radio' ) !== FALSE ) { if ( $this->radio_group( $var, $rules ) ) { $this->errors[] = $title . " wrong entry!"; } } //only letters & numbers are allowed if ( strpos( $rules, 'alpha_digit' ) !== FALSE ) { if ( $this->alpha_digit( $var ) ) { $this->errors[] = $title . ' field should contain only letters and/or numbers!<br />'; } } //custom chars if ( strpos( $rules, 'custom' ) !== FALSE ) { if ( $this->custom( $var, $rules ) ) { $this->errors[] = $title . " contains disallowed char(s)!<br />"; } } if ( !is_array( $var ) ) { $this->output[] = $title . ' : ' . $var . '<br />'; } else { $this->output[] = $title . ' : ' . implode( ',', $var ) . '<br />'; } } if ( !empty( $this->errors ) ) { return false; } else { return true; } } //required field private function required( $var ) { return empty( $var ); } //only text private function only_text( $var ) { if ( preg_match( '%[^A-Za-z ]%', $var ) ) return true; } //only numbers private function only_numbers( $var ) { if ( preg_match( '%[^0-9. ]%', $var ) ) return true; } // letters and numbers allowed private function alpha_digit( $var ) { if ( preg_match( '%[^A-Za-z0-9 ]%', $var ) ) return true; } //valid email private function valid_email( $var ) { if ( !filter_var( $var, FILTER_VALIDATE_EMAIL ) ) { return true; } } //min number of chars private function min( $var, $rules ) { preg_match( '%min=([0-9]{1,10})%', $rules, $match ); if ( empty( $match[ 1 ] ) ) return false; return strlen( $var ) < (int) $match[ 1 ] ? $match[ 1 ] : true; } //max number of chars private function max( $var, $rules ) { preg_match( '%max=([0-9]{1,10})%', $rules, $match ); if ( empty( $match[ 1 ] ) ) return false; return strlen( $var ) > (int) $match[ 1 ] ? $match[ 1 ] : true; } //check checkbox value public function single_checkbox( $var, $rules ) { preg_match( '%value:[0-9A-Za-z]{1,20}%', $rules, $match ); $value = str_replace( 'value:', '', $match[ 0 ] ); if ( !empty( $var ) ) if ( $var != $value ) { return true; } } //check checkboxes group value private function multi_checkbox( $var, $rules ) { if ( preg_match( '%values:[()0-9A-Za-z,]{1,200}%', $rules, $match ) ) { $replace = array( 'values:', '(', ')' ); $values = str_replace( $replace, '', $match[ 0 ] ); $values_array = explode( ',', $values ); } if ( !empty( $var ) ) foreach ( $var as $val ) { if ( !in_array( $val, $values_array ) ) return true; } } //check radio group values private function radio_group( $var, $rules ) { if ( preg_match( '%values:[()0-9A-Za-z,]{1,200}%', $rules, $match ) ) { $replace = array( 'values:', '(', ')' ); $values = str_replace( $replace, '', $match[ 0 ] ); $values_array = explode( ',', $values ); } if ( !empty( $var ) ) if ( !in_array( $var, $values_array ) ) return true; } //check custom private function custom( $var, $rules ) { if ( preg_match( '%\[.+\]%', $rules, $match ) ) { $rule = $match[ 0 ]; } if ( preg_match( "%" . $rule . "%", $var ) ) return true; } } ?>
upotreba i hm... dokumentacija
PHP kôd:
<?php error_reporting(0); session_start(); $prefix='frm_'; include('validator.php'); $validator= new Validator($prefix);
?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head>
<body> <form action="" method="post"> Please enter your name: <br /> <input name="frm_name" type="text" value="<?php echo $_SESSION["frm_name"]; ?>" /> <br /> Please enter your email: <br /> <input name="frm_email" type="text" value="<?php echo $_SESSION["frm_email"]; ?>" /> <br /> Please enter some number: <br /> <input name="frm_number" type="text" value="<?php echo $_SESSION["frm_number"]; ?>" /> <br /> I agree with terms and conditions: <input name="frm_terms" type="checkbox" value="1" <?php if($_SESSION['frm_terms']=='1') echo 'checked' ?> /> <br /> Vehicle:<br /> <input type="checkbox" name="frm_vehicle[]" value="Bike" <?php if(in_array('Bike',$_SESSION["frm_vehicle"])) echo 'checked'; ?> >I have a bike<br> <input type="checkbox" name="frm_vehicle[]" value="Car" <?php if(in_array('Car',$_SESSION["frm_vehicle"])) echo 'checked'; ?> >I have a car<br> <input type="checkbox" name="frm_vehicle[]" value="Plane"<?php if(in_array('Plane',$_SESSION["frm_vehicle"])) echo 'checked'; ?> >I have a plane<br> <br /> Gender:<br /> <input type="radio" name="frm_gender" value="male" <?php if($_SESSION['frm_gender']=='male') echo 'checked' ?> >Male<br /> <input type="radio" name="frm_gender" value="female"<?php if($_SESSION['frm_gender']=='female') echo 'checked' ?> >Female<br /> <br /> How did you hear about us: <br /> <select name="frm_how"> <option value="0">Please select one option</option> <option value="Google">Google</option> <option value="Radio">Radio</option> <option value="TV">TV</option> <option value="Other">Other</option> </select> <br /> Numbers and text:<br /> <input name="frm_numbtext" type="text" value="<?php echo $_SESSION['frm_numbtext']; ?>" /> <br /> Message:<br /> <textarea name="frm_message" cols="" rows="" style="width:250px;height:130px;"><?php echo $_SESSION['frm_message']; ?></textarea> <br /><br /> <input name="submit" type="submit" value="Submit" /> </form> <?php
if(isset($_POST['submit']))
{
// Set variable description titles (used for error messages), names, and validation rules + type of fields (checkboxes and radio buttons). /* req=required field text=only letters are allowed min = minimal number of characters max = maximal number of characters valid_email = obviously... :) num = only numeric chars alpha_digit = numbers&letters allowed custom = format:[^your custom list of allowed characters in field]
types: check_single - single check box check_multi - checkbox group radio - radio buttons group
//rules for check box -> check_single -> one checkbox, value:-> check for exact value! check_multi -> checkbox group values:-> check for exact values!
*/ $vars = array ( array("Name", 'req|text|min=6|max=20' , $_POST['frm_name']), array("Email", 'valid_email' , $_POST['frm_email']), array("Test Number", 'req|num|max=4' , $_POST['frm_number']), //single checkbox setup -> additional fields: type, checked value array("Terms and Conditions", 'req|type:check_single|value:1' , $_POST['frm_terms']), //single checkbox setup -> additional fields in rules section: type, multiple checkboxes, checked values array("Vehicle", 'req|type:check_multi|values:(Bike,Car,Plane)' , $_POST['frm_vehicle']), // Radio button group setup -> additional fields in rules section: type, checked values array("Gender", 'req|type:radio|values:(male,female)' , $_POST['frm_gender']), // select dropdown setup array("How did you hear about us", 'req|text' , $_POST['frm_how']), //letters and text validation array("Numbers and text", 'req|alpha_digit' , $_POST['frm_numbtext']),
//custom regex setup -> add custom ALLOWED characters, if variable contains any other chars - validation will fail -> format: [^custom chars] array("Message", 'req|custom[^A-Za-z0-9-,.\r\n!?= ]' , $_POST['frm_message'])
);
if(!$validator->process($vars)) { foreach ($validator->errors as $error) { echo $error.'<br />'; } } else
{ foreach ($validator->output as $line) { echo $line; } }
}
?> </body> </html>
Uskoro - jos suvisnih klasa.
Samo, moracu da smislim nesto malo egzoticnije i koliko-toliko korisnije, kao predmet klase.
Da, hvala na ovom linku za bjutifajer... jes' ga ulepsao, skoro ko pravi koder da je pis'o...
|