php - Limit string length with RegEx -


i wrote regex checking input number form:

if (!preg_match("/^0\d{10}+|^9\d{9}+/",$_post['number'])){    echo "error"; }else{    echo "ok"; } 

this code check minimum length if length more 10 or 9 characters, regex cannot work !

what should ? should check strlen after regex or can limit maximum length ?

update:

the string length should 10 characters if start 0 , should 9 characters if start 9, , should return false on ways (more or less length, start different numbers , ...)

possibly want anchor string @ end using $. can drop + after numeric quantifier:

^(?:0\d{9}|9\d{8})$ 

see test @ regex101.com


Comments