0
유효성 검사를 위해 주어진 필드에 대해 이와 같이 작성된 함수. 검토하고 가치있는 프로젝트 내 기능을 개선하십시오함수를 사용하여 필드의 유효성을 검사하는 더 나은 방법
내 필드
try{
$message = 'First Name accepts keyboard characters only.';
doValidateField ($firstName, 'First Name', true, 100, '/^([a-zA-Z0-9._\- #,^&`~<>:[email protected]$(){}\"\';\*\[\]?%| \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);
}
}
뭔가 작동하지 않습니까? 아니면 http://codereview.stackexchange.com을 찾으십니까? – deceze
"더 나은"감각? 현재 버전에서 어떤 문제점을 발견 할 수 있습니까? –
IsEmail(), IsStrLen()과 같은보다 구체적인 기능 (작고 쉬운 기능)을 작성해야합니다. 그렇게하기 만하면 모든 테스트를 "반복"하고 작은 함수를 호출하는 것입니다. – djot