validation - Securely validate php form field for 5-digit numeric value -


i have functioning form page user can update zip code based on username, saved session variable.

everything works except new feature validate zip code. have html5 pattern in place if enter 4 digits, prompts them follow 5 digit format. however, i"m required validate securely on server side well.

i've opted regular expression match 5 digits between 0 , 9, , i've nested in if statement being used preform sql queries. issue if enter 123 zip code, still submits.

obviously, need submit if it's 5 digits, otherwise print error message. i'm wondering if it's in structure or syntax.

here's code, help:

if (isset($_session['missingzipuser'])) {  if(!preg_match("/^[0-9]{5}$/", $_post['zip'])) { echo "the zip code must 5-digit number."; }else{    //query updating main table first   $sql = " update " . table. "       set zip= ?       uname ?";   $stmt = odbc_prepare($connect, $sql);   $success = odbc_execute($stmt, array($_post['zip'],   $_session['missingzipuser']));    //query updating both fields in secondary table   $sql2 = " update " . table. "       set zip1= ?, zip2= ?       uname= ?";   $stmt2 = odbc_prepare($connect, $sql2);   $success2 = odbc_execute($stmt2, array($_post['zip'], $_post['zip'],   $_session['missingzipuser'])); } } 

update contrary of comments below, update code still passing info successful.

just fix condition , go:

if (isset($_session['missingzipuser']) && isset($_post['zip'])) {     if (is_string($_post['zip']) && 1 === preg_match("/^[0-9]{5}$/", $_post['zip'])) {         // update record in database     } else {         echo "the zip code must 5-digit number.";     } } 

you can simplify regular expression:

if (isset($_session['missingzipuser']) && isset($_post['zip'])) {     if (is_string($_post['zip']) && 1 === preg_match("/^\d{5}$/", $_post['zip'])) {         // update record in database     } else {        echo "the zip code must 5-digit number.";     } } 

note pointed out, both original simplified regular expression verify whether string 5-digit number. not 5-digit numbers valid zip codes.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -