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