'Laravel validation for numbers and hyphens

How do you validate the numeric values with Hypen's (-) ? I just want to validate the numeric value with Hypen's and without Hypen's. Thank you so much !

For example 555 or 5-55



Solution 1:[1]

You may use preg_match with the regex pattern ^[0-9-]+$:

$input = "867-5309";
if (preg_match("/^[0-9-]+$/", $input)) {
    echo "MATCH";  // prints MATCH
}

Solution 2:[2]

I suggest for this purpose from the site:

https://www.phpliveregex.com/

use

For example, I wrote a preg_match in the link: ?

https://www.phpliveregex.com/p/Ep6

You can see

My suggested code:

$input = "867-5309";
echo preg_match("/^[0-9-]+$/", $input) ? 'MATCH' : 'Not MATCH';

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Biegeleisen
Solution 2 Dharman