对于给定字段的验证,我写了这样的函数。请检查并改进我的功能,使项目值得
我的领域
try{
$message = 'First Name accepts keyboard characters only.';
doValidateField ( $firstName, 'First Name', true, 100, '/^([a-zA-Z0-9._\- #,^&`~<>:!@$(){}\"\';\*\[\]?%| \n \r \t]*)$/', $message );
} catch ( Exception $e ) {
$errorMessage = $e->getMessage ();
echo $errorMessage;
}
功能
/*
* The following function is validates the fields
* @params unknown values $fieldValue, $fieldName, $required, $maxLength, $mask, $message
* @return tables rows $rowResponse
*/
function doValidateField($fieldValue, $fieldName, $required, $maxLength, $mask, $message) {
// Checking the required field is empty or not
if ($required) {
if (strlen ( $fieldValue ) <= 0) {
throw new Exception ( $fieldName . " is required." );
}
}
// Check field length is not greater than allowed length
if (strlen ( $fieldValue ) > $maxLength) {
throw new Exception ( $fieldName . " cannot be greater than " . $maxLength . " characters." );
}
// check if specified mask exists in the field
if (! (preg_match ( $mask, $fieldValue ))) {
throw new Exception ( $message );
}
}
答案 0 :(得分:0)
这可能与你想要的答案相比没那么具体,但我建议你看一下Zend_Validator类使用的一些模式和想法。 (它说“Zend”,但它不是PHP的默认部分。)
关于那个说明: