If no messages were found in an array then it is returning 2 instead of null PHP
I am appending messages to array and calling functions to append array but while calling the functions there were no messages found in array also it is showing them as 2 value those are Array ( [0] => [1] => )
$errorMessages = array ();
$isError = false;
$middleName = $_POST [ 'middleName' ];
$middleName = trim( stripslashes( $middleName ) );
//validation for warrant id
$warrantId = $_POST [ 'warrantId' ];
if ( $warrantId == null || ( strlen( $warrantId ) ) <= 0 ) {
$errorMessages[] = "Warrant Id is required";
} else {
$message = 'enter keyboard characters only for warrant Id.';
$warrantId = trim( stripslashes( $warrantId ) );
$x = checkLength($warrantId, 'WarrantId', 1);
$errorMessages[] = $x;
//$errorMessages[] = checkRegEx( $warrantId, '/^([a-zA-Z0-9._\- #,^&`~<>:!@$(){}\"\';\*\[\]?%| \n \r \t]*)$/', $message );
}
$errorMsg = count($errorMessages);
print_r($errorMsg);
functions
/*
* Checks the field length is not greater than allowed length
* @params unknown values $fieldValue, $fieldName, $maxLength
* @return tables rows $rowResponse
*/
function checkLength($fieldValue, $fieldName, $maxLength) {
$errorMsg = NULL;
if (strlen ( $fieldValue ) > $maxLength) {
$errorMsg = $fieldName . " cannot be greater than " . $maxLength . " characters.";
}
return $errorMsg;
}
/*
* Checks the given field value to match with regular expression or not
* @params unknown values $fieldValue, $mask, $message
* @return tables rows $rowResponse
*/
function checkRegEx($fieldValue, $regEx, $message) {
$errorMsg = NULL;
if (! (preg_match ( $regEx, $fieldValue ))) {
$errorMsg = $message;
}
return $errorMsg;
}
if no messages are found in array then also it is returning it as 2
the
checkLengthandcheckRegExfunctions are returningNULLand adding that to the array. You should only assign the result of the function calls if they do not returnNULL.