Thinkphp automatic verification-callback function verification form field length usage

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. The callback function that has never been known for automatic verification can also transmit parameters, Nima, and quanyi's tp, the manual is not clearly written. However, it is much easier to verify the length of a field. This code is summed up by myself during the project process and is absolutely usable.

Tip:
1. The regular modifier/u In the callback function treats the Matching content as a UTF-8 encoded character. Therefore, do not remove the/u modifier to be able to determine the length of Chinese characters.
2. This function is used to determine whether a string is located in a certain range. You can assemble it a little and change it to determine the length of a string at least, and the maximum length cannot exceed. I will not change it.
Class UserModel extends Model {
/**
* Author: Fan Fei
* Duration: 20131120
**/
Protected $ _ validate = array (
Array ('username', 'require ', 'username is required! '),
Array ('username', 'checklength', 'username length must be 3-15 characters long! ', 0, 'callback', 3, array (3, 15 )),
Array ('Password', 'require ', 'password is required! '),
Array ('confirmpwd ', 'require', 'confirm that the password is required! '),
Array ('email ', 'require', 'email is required! ',),
Array ('email ','/^ [\ w \-\.] + @ [\ w \-\.] + (\. \ w +) + $/', 'email format error! ', 2, 'regex '),
);
/**
* Verify whether the character length is in a certain interval,
* $ Str: Content received by the form field,
* $ Min: minimum length,
* Max: Maximum length,
**/
Function checklength ($ str, $ min, $ max ){
Preg_match_all ("/./u", $ str, $ matches );
$ Len = count ($ matches [0]);
If ($ len <$ min | $ len> $ max ){
Return false;
} Else {
Return true;
}
}
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.