I need to create a regular expression to validate comma separated numeric values.
They should look like: 1,2,3,4,5 etc….
The value must be either a single digit like: 1 no empty spaces before or after, no commas before or after.
Or… multiple numerical values separated by commas. First and last characters must be a number.
I have the following code but it only checks for numbers and commas in no particular order:
How can I change the regular expression below to fit the above description?
Thank you!
// get posted value
if(isset($_POST['posted_value']))
{
$sent_value = mysqli_real_escape_string($conn, trim($_POST['posted_value']));
if(preg_match('/^[0-9,]+$/', $posted_value))
{
$what_i_need = $posted_value;
}
else
{
$msg .= $not_what_i_need;
}
}
else
{
$msg .= $posted_value_not_set;
}
This should do it:
Explanation:
If you allow multi-digit numbers, then change
\dto\d+.