php - preg_match for number error? -


i have form, user must fill in id card number. id number of cards has length of 16 character. want check whether string entered correct number length of 16 characters?

i have code:

<?php $nik=1234567891234567;  if(!preg_match('/^[1-9][0-9]{16}$/', $nik)){     echo 'nooooo';     exit; }else{     echo 'yesss';     exit; } ?> 

the result echoed nooooo, there wrong regex?

the regex problem

what want this:

<?php $nik=1234567891234567;  var_dump($nik);  if(preg_match('/^[1-9]\d{15}$/', $nik)){     echo "contains numbers";     exit; }else{     echo "contains non-numeric characters";     exit; } 

here's demo.

this match string 16 characters; first can 1-9, rest can digit. regex, /^[1-9][0-9]{16}$/, matches character in range 1-9, 16 characters in range 0-9, total of 17 characters.

integer size

also, code has logical flaw: number larger maximum integer value on 32-bit system, stated in the documentation. largest value on system can determined checking constant php_int_max. 32-bit system, 2147483647. has fewer 16 characters, code not work reliably on 32-bit system.

strings versus integers

also, post said you're getting info user via form. in case, receiving string, not integer. example, if field named nik, access info $_post['nik'] (for post form) or $_get['nik'] (for form). then, use string; it's not number, anyway.

other considerations

you're checking 16-character number. sounds involving credit cards. if doing credit card processing, should know there major security implications , compliance issues related processing cards on server. can't give legal advice, , how process credit card broad topic site. this: if credit card data, do not want way unless have large budget compliance issues, auditing, , like. should using paypal, stripe, or similar vendor handle this.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -