I have a string to check and if it is only numbers and digits perform another action else exit;
$data = 123412347JB91742F;
if(preg_match('/[A-Z1-9]/', $data )) {
echo $data;
}
else {
exit;
}
this works fine but if I add anything to $data like $ or any other thing it still prints the value. What is wrong with this code?
Edit:
$data = preg_replace('/\-/', '', '1234-1234-JB91-8742F');
if(preg_match('/^[A-Z1-9]+$/', $data )) {
echo $data;
} else {
exit;
}
Your code should look like this:
Do you want to match zeros as well as lower case characters? Then your regex should look like this: