Search code examples
phpposix-ere

PHP: How to use ereg to validate input text AND string length simultaneously?


I'm using ereg in the followin way to validate a field which can contain only numbers from 0 to 9:

if(eregi("^([0-9])*$", $mynumber)) return true;

However the string must have between 8 and 10 characeters. Is it possible to improve the same ereg usage to check for a valid string length as well?

Thanks in advance.. all ereg tutorials seem to be traditional chinese to me. :S


Solution

  • ereg*() functions are deprecated, and will be removed in a future version:

    if(preg_match("/^([0-9]){8,10}$/", $mynumber)) return true;