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})$ 

demo

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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -