كيفية ربط الكلاس بالحقول
اخواني الكرام هل هذا الكلاس صحيح ام خطأ
<?php class input_security { private $post; public function postf($post_fild) { $this->post = $post_fild; if ($this->post <= 10) { return $this->post; } } public function notfound ($fil) { if ($fil == '' || empty($fil)) { return $this->post; } } } ?> <form method="post"> <input type="text" name="username" /> <input type="submit" name="submit" value="Insert" /> </form> <?php if ($_POST['submit']) { $post_username = $_POST['username']; $new_class = new input_security(); if ($new_class->postf($post_username)) { echo "اقل من 10 حروف"; }else if ($new_class->notfound($post_username)) { echo "فاااااااارغ"; } } ?> اريد اني اعمل كلاس التحقق من الحقول وهكذا
2 إجابات
مش عاوز يظبط معايه خالص الكلاس :(
بواسطة: Mohamed.Ayoub.101 تعديل: Mohamed.Ayoub.101 0 من 0 أشخاص وجدو هذه الاجابة مفيدة. هل وجدتها؟ نعم لا
شوف الشرح ده
وشوف الكلاس دا class Validate_fields { var $fields = array(); var $messages = array(); var $check_4html = false; var $language; function Validate_fields() { $this->language = "uk"; $this->create_msg(); } function validation() { $status = 0; foreach ($this->fields as $key => $val) { $name = $val['name']; $length = $val['length']; $required = $val['required']; $num_decimals = $val['decimals']; $ver = $val['version']; switch ($val['type']) { case "email": if (!$this->check_email($name, $key, $required)) { $status++; } break; case "number": if (!$this->check_num_val($name, $key, $length, $required)) { $status++; } break; case "decimal": if (!$this->check_decimal($name, $key, $num_decimals, $required)) { $status++; } break; case "date": if (!$this->check_date($name, $key, $ver, $required)) { $status++; } break; case "url": if (!$this->check_url($name, $key, $required)) { $status++; } break; case "text": if (!$this->check_text($name, $key, $length, $required)) { $status++; } break; } if ($this->check_4html) { if (!$this->check_html_tags($name, $key)) { $status++; } } } if ($status == 0) { return true; } else { $this->messages[] = $this->error_text(0); return false; } } function add_text_field($name, $val, $type = "text", $required = "y", $length = 0) { $this->fields[$name]['name'] = $val; $this->fields[$name]['type'] = $type; $this->fields[$name]['required'] = $required; $this->fields[$name]['length'] = $length; } function add_num_field($name, $val, $type = "number", $required = "y", $decimals = 0, $length = 0) { $this->fields[$name]['name'] = $val; $this->fields[$name]['type'] = $type; $this->fields[$name]['required'] = $required; $this->fields[$name]['decimals'] = $decimals; $this->fields[$name]['length'] = $length; } function add_link_field($name, $val, $type = "email", $required = "y") { $this->fields[$name]['name'] = $val; $this->fields[$name]['type'] = $type; $this->fields[$name]['required'] = $required; } function add_date_field($name, $val, $type = "date", $version = "us", $required = "y") { $this->fields[$name]['name'] = $val; $this->fields[$name]['type'] = $type; $this->fields[$name]['version'] = $version; $this->fields[$name]['required'] = $required; } function check_url($url_val, $field, $req = "y") { if ($url_val == "") { if ($req == "y") { $this->messages[] = $this->error_text(1, $field); return false; } else { return true; } } else { $url_pattern = "http\:\/\/[[:alnum:]\-\.]+(\.[[:alpha:]]{2, 4})+"; $url_pattern .= "(\/[\w\-]+)*"; // folders like /val_1/45/ $url_pattern .= "((\/[\w\-\.]+\.[[:alnum:]]{2, 4})?"; // filename like index.html $url_pattern .= "|"; // end with filename or ? $url_pattern .= "\/?)"; // trailing slash or not $error_count = 0; if (strpos($url_val, "?")) { $url_parts = explode("?", $url_val); if (!preg_match("/^".$url_pattern."$/", $url_parts[0])) { $error_count++; } if (!preg_match("/^(&?[\w\-]+=\w*)+$/", $url_parts[1])) { $error_count++; } } else { if (!preg_match("/^".$url_pattern."$/", $url_val)) { $error_count++; } } if ($error_count > 0) { $this->messages[] = $this->error_text(14, $field); return false; } else { return true; } } } function check_num_val($num_val, $field, $num_len = 0, $req = "n") { if ($num_val == "") { if ($req == "y") { $this->messages[] = $this->error_text(1, $field); return false; } else { return true; } } else { $pattern = ($num_len == 0) ? "/^\-?[0-9]*$/" : "/^\-?[0-9]{0, ".$num_len."}$/"; if (preg_match($pattern, $num_val)) { return true; } else { $this->messages[] = $this->error_text(12, $field); return false; } } } function check_text($text_val, $field, $text_len = 0, $req = "y") { if ($text_val == "") { if ($req == "y") { $this->messages[] = $this->error_text(1, $field); return false; } else { return true; } } else { if ($text_len > 0) { if (strlen($text_val) > $text_len) { $this->messages[] = $this->error_text(13, $field); return false; } else { return true; } } else { return true; } } } function check_decimal($dec_val, $field, $decimals = 2, $req = "n") { if ($dec_val == "") { if ($req == "y") { $this->messages[] = $this->error_text(1, $field); return false; } else { return true; } } else { $pattern = "/^[-]*[0-9][0-9]*\.[0-9]{".$decimals."}$/"; if (preg_match($pattern, $dec_val)) { return true; } else { $this->messages[] = $this->error_text(12, $field); return false; } } } function check_date($date, $field, $version = "us", $req = "n") { if ($date == "") { if ($req == "y") { $this->messages[] = $this->error_text(1, $field); return false; } else { return true; } } else { if ($version == "eu") { $pattern = "/^(0[1-9]|[1-2][0-9]|3[0-1])[-](0[1-9]|1[0-2])[-](19|20)[0-9]{2}$/"; } else { $pattern = "/^(19|20)[0-9]{2}[-](0[1-9]|1[0-2])[-](0[1-9]|[1-2][0-9]|3[0-1])$/"; } if (preg_match($pattern, $date)) { return true; } else { $this->messages[] = $this->error_text(10, $field); return false; } } } function check_email($mail_address, $field, $req = "y") { if ($mail_address == "") { if ($req == "y") { $this->messages[] = $this->error_text(1, $field); return false; } else { return true; } } else { if (preg_match("/^[0-9a-z]+(([\.\-_])[0-9a-z]+)*@[0-9a-z]+(([\.\-])[0-9a-z-]+)*\.[a-z]{2, 4}$/i", strtolower($mail_address))) { return true; } else { $this->messages[] = $this->error_text(11, $field); return false; } } } function check_html_tags($value, $field) { if (preg_match("/[<](\w+)((\s+)(\w+)[=]((\w+)|(\"\.\")|('\.')))*[>]/", $value)) { $this->messages[] = $this->error_text(15, $field); return false; } else { return true; } } function create_msg() { $the_msg = ""; asort($this->messages); reset($this->messages); foreach ($this->messages as $value) { $the_msg .= $value."<br>\n"; } return $the_msg; } function error_text($num, $fieldname = "") { $fieldname = str_replace("_", " ", $fieldname); switch ($this->language) { case "de": $msg[0] = "Verbessern Sie bitte folgende Fehler:"; $msg[1] = "Das Feld <b>".$fieldname."</b> ist leer."; $msg[10] = "Das Datum im Feld <b>".$fieldname."</b> ist ungütig."; $msg[11] = "Die Email Adresse im Feld <b>".$fieldname."</b> ist ungültig."; $msg[12] = "Der Wert im Feld <b>".$fieldname."</b> ist ungültig."; $msg[13] = "Der Text im Feld <b>".$fieldname."</b> ist zu lang."; $msg[14] = "Die internetadresse im Feld <b>".$fieldname."</b> ist ungültig."; $msg[15] = "Das Feld <b>".$fieldname."</b> enthält html-Zeichen, die sind nicht erlaubt."; break; case "nl": $msg[0] = "Corrigeer de volgende fouten:"; $msg[1] = "Het veld <b>".$fieldname."</b> mag niet leeg zijn."; $msg[10] = "Het datum in veld <b>".$fieldname."</b> is niet geldig."; $msg[11] = "Het e-mail adres in veld <b>".$fieldname."</b> is niet geldig."; $msg[12] = "De waarde van veld <b>".$fieldname."</b> is niet geldig."; $msg[13] = "De tekst in veld <b>".$fieldname."</b> is te lang."; $msg[14] = "De internetadres in het veld <b>".$fieldname."</b> is niet geldig."; $msg[15] = "In het veld <b>".$fieldname."</b> is html-code, dit is niet toegestaan."; break; case "dk": $msg[0] = "Ret følgende fejl:"; $msg[1] = "Feltet <b>".$fieldname."</b> er tomt."; $msg[10] = "Datoen i feltet <b>".$fieldname."</b> er ikke gyldig."; $msg[11] = "E-mail-adressen i feltet <b>".$fieldname."</b> er ikke gyldig."; $msg[12] = "Værdien i feltet <b>".$fieldname."</b> er ikke gyldig."; $msg[13] = "Teksten i feltet <b>".$fieldname."</b> er for lang."; $msg[14] = "URL'en i feltet <b>".$fieldname."</b> er ikke gyldig."; $msg[15] = "Feltet <b>".$fieldname."</b> indeholder HTML-koder, hvilket ikke er tilladt."; break; default: $msg[0] = "Please correct the following error(s):"; $msg[1] = "The field <b>".$fieldname."</b> is empty."; $msg[10] = "The date in field <b>".$fieldname."</b> is not valid."; $msg[11] = "The e-mail address in field <b>".$fieldname."</b> is not valid."; $msg[12] = "The value in field <b>".$fieldname."</b> is not valid."; $msg[13] = "The text in field <b>".$fieldname."</b> is too long."; $msg[14] = "The url in field <b>".$fieldname."</b> is not valid."; $msg[15] = "Ther is html code in field <b>".$fieldname."</b>, this is not allowed."; } return $msg[$num]; } } ?>ودا كلاس للايميل والحجات دى <?php /** * Validation Class * v3.0 * Last Updated: Mar 28, 2011 * URL: http://www.nickyeoman.com/blog/php/104-php-validation-class * * Changelog: * v2 now works with PHP 5.3 and up * v3 is easy to intergrate into CI as a library (renamed) + bug fixes **/ class Validate { /** * If an email is Valid it returns the parameter * other wise it will return false * $email is the email address **/ function isEmail($email) { //email is not case sensitive make it lower case $email = strtolower($email); //check if email seems valid if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email)) { return $email; } return false; } /** * Checks if there are 7 or 10 numbers, if so returns cleaned parameter(no formating just numbers) * other wise it will return false * $phone is the phone number * $ext if set to true return an array with extension separated **/ function isPhone($phone, $ext = false) { //remove everything but numbers $numbers = preg_replace("%[^0-9]%", "", $phone ); //how many numbers are supplied $length = strlen($numbers); if ( $length == 10 || $length == 7 ) { //Everything is find and dandy $cleanPhone = $numbers; if ( $ext ) { $clean['phone'] = $cleanPhone; return $clean; } else { return $cleanPhone; } } elseif ( $length > 10 ) { //must be extension //checks if first number is 1 (this may be a bug for you) if ( substr($numbers,0,1 ) == 1 ) { $clean['phone'] = substr($numbers,0,11); $clean['extension'] = substr($numbers,11); } else { $clean['phone'] = substr($numbers,0,10); $clean['extension'] = substr($numbers,10); } if (!$ext) { //return string if (!empty($clean['extension'])) { $clean = implode("x",$clean); } else { $clean = $clean['phone']; } return $clean; } else { //return array return $clean; } } return false; } /** * Canadian Postal code * thanks to: http://roshanbh.com.np/2008/03/canda-postal-code-validation-php.html **/ function isPostalCode($postal) { $regex = "/^([a-ceghj-npr-tv-z]){1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}$/i"; //remove spaces $postal = str_replace(' ', '', $postal); if ( preg_match( $regex , $postal ) ) { return $postal; } else { return false; } } /** * Checks for a 5 digit zip code * Clears extra characters * returns clean zip **/ function isZipCode($zip) { //remove everything but numbers $numbers = preg_replace("[^0-9]", "", $zip ); //how many numbers are supplied $length = strlen($numbers); if ($length != 5) { return false; } else { return $numbers; } } } بواسطة: MacOS 0 من 0 أشخاص وجدو هذه الاجابة مفيدة. هل وجدتها؟ نعم لا |
انا لا ارى سبب انك تستعمل مثل هذه الطرق
13 / أغسطس / 2012 الساعة 17:11
انا فهمت الكلاس التاني بس المشكله متحلتش عندي وهي طريقى استخدام الكلاس في حاجه متغلبطه عندي لو عاوز اعمل تحقق لحقل اعملها ازاي واستخدمها مع حقل ازاي
15 / أغسطس / 2012 الساعة 0:0