preg_match() expects parameter to be long string given
php, preg-match, regex
Solution
preg_match check only one subject at a time.
Try following code:
function creditcard($nums)
{
$regexp="/^[0-9]{4}$/";
for ($i = 0; $i < 4; $i++)
if (! preg_match($regexp, $nums[$i]))
return FALSE;
return TRUE;
}
$creditCard1 = $_POST['creditCard1'];
$creditCard2 = $_POST['creditCard2'];
$creditCard3 = $_POST['creditCard3'];
$creditCard4 = $_POST['creditCard4'];
$ok = creditcard([$creditCard1, $creditCard2, $creditCard3, $creditCard4]);
if (! $ok)
echo '<script type="text/javascript">alert("Enter a valid credit card number.")</script>';
Problem
I am checking credit card fields to make sure they contain four digits each. ``` function creditcard ($creditCard1,$creditCard2,$creditCard3,$creditCard4) { $regexp="/^[0-9]{4}$/"; if (preg_match($regexp,$creditCard1,$creditCard2,$creditCard3,$creditCard4))//line 18 { return TRUE; } else { echo'<script type="text/javascript">alert("Enter a valid credit card number.")</script>'; return FALSE; } } $creditCard1=$_POST['creditCard1']; $creditCard2=$_POST['creditCard2']; $creditCard3=$_POST['creditCard3']; $creditCard4=$_POST['creditCard4']; $creditcard=creditcard($creditCard1,$creditCard2,$creditCard3,$creditCard4); ``` I am getting this error. ``` Warning: preg_match() expects parameter 4 to be long, string given in C:\xampp\htdocs\SFASC2\aa.php on line 18 ``` I can't work out what I'm doing wrong, thanks. There are many questions like this, but none giving me the solution.. and I've been hitting my head against a wall, no doubt overlooking the obvious.