Php form verification code

Source: Internet
Author: User
Tags php form

Provides a php form verification and validation code.

Function validate ($ value, $ onoff = 0, $ type = 3, $ len = 100, $ msg = 'fill in error '){


If (trim ($ value )! = "") {// If there is any content

If (! Is_int ($ len ))
Exit ("<script language = javascript> alert ('length parameter is not number'); </script> ");

Preg_match_all ("/[xa0-xff]?. /", $ Value, $ arr); // mix Chinese and English to calculate the string length
$ QswhLen_v = count ($ arr [0]);
If ($ qswhLen_v> $ len)
{
Exit ("<script language = javascript> alert ('". $ msg. "n length error! '); </Script> ");
 
}

If ($ type = 0) {// if the type is 0, check whether the data is a number.
If (! Eregi ('^ [0-9] + $', $ value )){
Exit ("<script language = javascript> alert ('". $ msg. "n must be a number! '); </Script> ");
}
}
Else if ($ type = 1) {// if the type is 1, check whether the data is a letter.
If (! Eregi ('^ [a-zA-Z] + $', $ value )){
Exit ("<script language = javascript> alert ('". $ msg. "n must be a letter! '); </Script> ");
}
}
Else if ($ type = 2) {// if the type is 2, check whether the data is in Chinese.
If (! Eregi ("^ [". chr (0xa1). "-". chr (0xff). "] + $", $ value )){
Exit ("<script language = javascript> alert ('". $ msg. "n must be Chinese! '); </Script> ");
}
}
Else if ($ type = 3) {// if the type is 3, check whether the data is letters and numbers.
If (! Eregi ('^ [0-9a-zA-Z] + $', $ value )){
Exit ("<script language = javascript> alert ('". $ msg. "n must be letters and numbers! '); </Script> ");
}
}
Else if ($ type = 4) {// email check if the type is 4
If (! Eregi ("^ [_. 0-9a-zA-Z-] + @ ([0-9a-zA-Z _-] +.) + [a-zA-Z] $", $ value )){
Exit ("<script language = javascript> alert ('". $ msg. "n format error! '); </Script> ");
}
}
Else if ($ type = 5) {// Date Format check if the type is 5
If (! Eregi ('^ [1-2] {1} [0-9] {3}-[0-9] {2}-[0-9] {2 }', $ value )){
Exit ("<script language = javascript> alert ('". $ msg. "n format error! '); </Script> ");
}
}
Else if ($ type = 6) {// if the type is 6, check the ID card number format
If (! Preg_match ('/(^ ([d] {15} | [d] {18} | [d] {17} x) $)/', $ value )){
Exit ("<script language = javascript> alert ('". $ msg. "n format error! '); </Script> ");
}
}
Else if ($ type = 7) {// if the type is 7, check the phone number format
If (! Preg_match ("/^ (d {3}) | (d {3 }-))? (0d {2, 3}) | 0d {2, 3 }-)? [1-9] d {6, 7} $/", $ value )){
Exit ("<script language = javascript> alert ('". $ msg. "n format error! '); </Script> ");
}
}
Else if ($ type = 8) {// if the type is 8, check the zip code format
If (! Preg_match ("/^ [1-9] d {5} $/", $ value )){
Exit ("<script language = javascript> alert ('". $ msg. "n format error! '); </Script> ");
}
}
Else if ($ type = 9) {// check the url address format if the type is 9
If (! Preg_match ("/^ http: // [A-Za-z0-9] +. [A-Za-z0-9] + [/=? % -&_~ '@ []': +!] * ([^ <> ""]) * $/", $ Value )){
Exit ("<script language = javascript> alert ('". $ msg. "n format error! '); </Script> ");
}
}
Else if ($ type = 10) {// if the type is 10, there is no limit.

} Else {
Echo "<script language = javascript> alert ('category error'); </script> ";
Exit;
}

} Else {
If ($ onoff! = 0 ){
Echo "<script language = javascript> alert ('". $ msg. "n Add". $ msg. "'); </script> ";
Exit;
}
}

}


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.