php - How to add and OR statement correctly -
i trying comparison of serial numbers 20140831-123 or 20140831-1234 form can accept our new serial numbers contain 4 last numbers. far have tried elseif statement , or operator no results doing wrong? there way change reg expression accept 3 or 4 digits @ end of serial?
if($name == 'newserial1'){ $newserial1 = $_post['newserial1']; if($newserial1 != '') { if(!preg_match('/^([0-9]{8}-)([0-9]{3})$/', $newserial1) || (!preg_match('/^([0-9]{8}-)([0-9]{4})$/', $newserial1))) { $result['valid'] = false; $result['reason'][$name] = 'incorrect serial number.'; } } }
just use below regex match last 3 or 4 digits also,
^([0-9]{8}-)([0-9]{3,4})$
explanation:
^
asserts @ start.([0-9]{8}-)
captures 8 digits , following-
symbol.([0-9]{3,4})
remaining 3 or 4 digits captured second group.$
asserts @ end.
Comments
Post a Comment