Form validation class Validator for PHP

Source: Internet
Author: User
Tags chr commit count join php code split trim zip
The form verification file is saved as form_validate.phpPHP code <title> form Validation class Validator for phpβ</title>
<style>
Body,td{font:normal 12px verdana;color: #333333}
Input,textarea,select,td{font:normal 12px verdana;color: #333333; border:1px solid #999999; background: #ffffff}
Table{border-collapse:collapse;}
TD{PADDING:3PX}
input{height:20;}
Textarea{width:80%;height:50px;overfmin:auto;}
Form{display:inline}
</style>
<table align= "center" >
<form action= "form_validate.php" method= "POST" >
<tr>
<td> hint Mode:</td><td>
<select name= "Emode" >
<option value= "1" > Warning prompt box (client) </option>
<option value= "2" > Prompt box with red text hint </option>
<option value= "3" > Additional Red Tips </option>
<option value= "4" > Warning prompt box (server side) </option>
<option value= "5" > Text tip (server side) </option>
</select></td>
</tr>
<tr>
<td> real name: </td><td><input name= "Name" ></td>
</tr>
<tr>
<td> English Name: </td><td><input name= "Nick" ></td>
</tr>
<tr>
<td> Home: </td><td><input name= "homepage" ></td>
</tr>
<tr>
<td> Password: </td><td><input name= "Password" ></td>
</tr>
<tr>
<td> repeat: </td><td><input name= "Repeat" ></td>
</tr>
<tr>
<td> Mail: </td><td><input name= "Email" ></td>
</tr>
<tr>
<td> Mail: </td><td><input name= "Email1" ></td>
</tr>
<tr>
<td>qq:</td><td><input name= "QQ" ></td>
</tr>
<tr>
<td> ID: </td><td><input name= "card" ></td>
</tr>
<tr>
<td> Age: </td><td><input name= "Year" ></td>
</tr>
<tr>
<td> age 1:</td><td><input name= "Year1" ></td>
</tr>
<tr>
<td> Tel: </td><td><input name= "Phone" ></td>
</tr>
<tr>
<td> Mobile: </td><td><input name= "mobile" ></td>
</tr>
<tr>
<td> Birthday: </td><td><input name= "Birthday" ></td>
</tr>
<tr>
<td> zip Code: </td><td><input name= "Zip" ></td>
</tr>
<tr>
<td> zip Code: </td><td><input name= "Zip1" ></td>
</tr>
<tr>
<td> operating system: </td><td><select name= "OS" ><option value= "" > select the operating system you are using </option>< Option value= "Win98" >win98</option><option value= "Win2K" >win2k</option><option value= " WinXP ">WinXP</option></select></td>
</tr>
<tr>
<td> province:</td><td> Guangdong <input name= "province" value= "1" type= "Radio" > Shaanxi <input name= " Province "value=" 2 "type=" Radio "> Zhejiang <input name=" province "value=" 3 "type=" Radio "> Jiangxi <input name=" province "Value=" 4 "type=" Radio "></td>
</tr>
<tr>
<td> Hobby:</td><td> Sports <input name= "favorite" value= "1" type= "checkbox" > Internet access <input name= " Favorite "Value=" 2 "type=" checkbox "> Listening to music <input name=" Favorite "value=" 3 "type=" checkbox "> Reading <input name=" Favorite "Value=" 4 "type=" checkbox "></td>
</tr>
<td> Self Introduction: </td><td><textarea name= "Description" > Chinese is a word </textarea></td>
</tr>
<td> autobiography: </td><td><textarea name= "History" > Chinese is two bytes t</textarea></td>
</tr>
<tr>
&LT;TD colspan= "2" ><input name= "Submit" type= "Submit" value= "Confirm Submission" ></td>
</tr>
</form>
</table>
<script defer>
/*************************************************
Validator for phpβ client script
Code by I foshan people
wfsr@cunite.com
http://www.cunite.com
*************************************************/
function Disperror (items, messages, mode, separator) {
var IArray = items.split (separator);
for (Var i=iarray.length-1;i>=0;i--)
Iarray[i] = Getobj (Iarray[i]);
Messages = ("The following causes the commit failure: \t\t\t\t" + separator + messages). Split (separator);
Switch (mode) {
Case 2:
for (i=iarray.length-1;i>=0;i--)
Iarray[i].style.color = "Red";
Case 1:
Alert (Messages.join ("\ n"));
Iarray[0].focus ();
Break
Case 3:
for (i=iarray.length-1;i>=0;i--) {
try{
var span = document.createelement ("span");
Span.id = "__errormessagepanel";
Span.style.color = "Red";
Iarray[i].parentnode.appendchild (span);
span.innerhtml = Messages[i+1].replace (/\d+:/, "*");
}
catch (E) {alert (e.description);}
}
Iarray[0].focus ();
Break
}
} function Getobj (name) {
var objs = document.getelementsbyname (name);
return objs[objs.length-1];
}
</script>
<?php
Class validator{
/*************************************************
Validator for phpβ server-side script
Code by I foshan people
wfsr@cunite.com
http://www.cunite.com
*************************************************/
var $submit;
var $error _item, $error _message, $error _mode, $error _no;
function Validator ($submit _name = "Submit", $mode = 5) {
$this->submit = $submit _name;
$this->error_mode = $mode;
$this->error_no = 1;
} function Validate ($arr) {
if (! isset ($_post[$this->submit]) return false;
$this->error_mode = $_post["Emode"];
echo "<script defer>document.getelementsbyname (\" Emode\ ") [0].selectedindex =]. ($this->error_mode-1). "</script>";
if (Is_array ($arr)) {
$len = count ($arr);
for ($i = 0; $i < $len; $i + +) {
$this->is_valid ($arr [$i]);
}
if ($this->error_no > 1)
$this->display_error ();
} function Is_valid ($STR) {
$str = Split (",", $str);
if (count ($STR) < 3) return false;
$name = Trim ($str [0]);
$message = Trim ($str [1]);
$data _type = Trim ($str [2]);
$value = Trim ($_post[$name]); Switch ($data _type) {
Case "Compare":
Break
Case "Range":
Break
Case "Repeat":
Break
Default:
$method = "Is_". $data _type;
if (! $this-> $method ($value))
$this->add_error ($name, $message);
Break
}
function Add_error ($name, $message) {
$this->error_item. = ",". $name;
$this->error_message. = ",". $this->error_no. ":" . $message;
$this->error_no + +;
function Display_error () {
$this->error_item = ereg_replace ("^,+", "", $this->error_item);
$this->error_message = ereg_replace ("^,+", "", $this->error_message); Switch ($this->error_mode) {
Case 4:
$info = "Commit failed for the following reasons: \t\t\t\t,";
echo "<script>alert". Join (" \\n", Split (", ", $info. $this->error_message)). " \ ") </script>";
Print >>>end;
Break
Case 5:
echo "entered an error: <br/><ul><li>". Ereg_replace ("\b\d+:", "", Join ("</li><li>", Split (",", $this->error_message)). " </li></ul> ";
echo "<br/><a href= ' javascript:history.back () ' > Return </a>";
Exit
Break
Default:
echo "<script defer>disperror (\" ".) $this->error_item. "\", \"" . $this->error_message. "\", " . $this->error_mode. ", \", \ ") </script>";
Break
}
} function Is_email ($STR) {
Return Preg_match ("/^\w+ [-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/", $str);
} function Is_url ($STR) {
Return Preg_match ("/^http:\/\/[a-za-z0-9]+\.[ a-za-z0-9]+[\/=\?%\ -&_~ ' @[\]\ ': +!] * ([^<>\ "]) *$/", $str);
} function Is_qq ($STR) {
Return Preg_match ("/^[1-9]\d{4,8}$/", $str);
} function Is_zip ($STR) {
Return Preg_match ("/^[1-9]\d{5}$/", $str);
} function Is_idcard ($STR) {
Return Preg_match ("/^\d{15} (\d{2}[a-za-z0-9])" $/", $str);
} function Is_chinese ($STR) {
Return Ereg ("^[". Chr (0XA1). " -". Chr (0xFF)."] +$ ", $STR);
} function Is_english ($STR) {
Return Preg_match ("/^[a-za-z]+$/", $str);
} function Is_mobile ($STR) {
Return Preg_match (/^ (\d{3}\) | ( \d{3}\-))? 13\d{9}$/", $STR);
} function Is_phone ($STR) {
Return Preg_match (/^ (\d{3}\) | ( \d{3}\-))? (\ (0\d{2,3}\) |0\d{2,3}-)? [1-9]\d{6,7}$/", $STR);
} function Is_safe ($STR) {
Return (Preg_match ("/^" ([a-z]*|[ a-z]*|\d*| [-_\~!@#\$%\^&\*\.\ (\) \[\]\{\}<>\?\\\/\ ' \ "]*) |. {0,5}) $|\s/", $str)!= 0);
}
}
$v = new Validator ();
$v->validate ("name, the name only allows Chinese, Chinese", "Nick, only allowed English nicknames, 中文版", "homepage, homepage URL format is incorrect, url", "Password, Password does not comply with safety rules, safe "," email, mailbox format error, email, "QQ, QQ number does not exist, QQ", "card, ID number is incorrect, Idcard", "phone, phone number does not exist," phone "," Mobile, Mobile phone number does not exist, mobile "," Zip, postcode does not exist, zip ")";


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.