Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason why your code isn't working is not because of your boolean logic, but because of your use of <code>isset()</code>. In the case of a <code>&lt;input type="text"&gt;</code>, the <code>$_POST['fieldName']</code> will always be set, regardless of if the value is empty or not.</p> <p>Use <code>$_POST['fieldName'] != ''</code> instead to determine if the user has entered a value. <strong>DO NOT USE <code>empty()</code></strong>, as this will return any falsy value as empty (<code>0</code>, <code>000</code>, <code>false</code>, etc...).</p> <hr> <p>Personally, I rather use one <code>&lt;input type="type"&gt;</code> for the phone number. This is less annoying than making the user switch boxes, and also makes validation simpler. </p> <p>This example actually validates if the number follows <a href="http://en.wikipedia.org/wiki/NANP" rel="nofollow noreferrer">NANP</a> rules. I find it absolutely ridiculous that so many applications/websites oversees this validation step.</p> <pre><code>// Did the user post a number? if($_POST['phone'] != '') { // Get only the numbers, we don't care how the user formatted their number $_POST['phone'] = preg_replace('/[^0-9]/', '', $_POST['phone']); // Is it a valid NANP phone number? if(preg_match('/^1?[2-9][0-8][0-9][2-9][0-9]{6}$/i', $_POST['phone']) === 1) { echo "Valid NANP phone number"; // Trim the leading one $_POST['phone'] = ltrim($_POST['phone'], '1'); // Format as wanted $_POST['PhoneNumber'] = '+01'.substr($_POST['phone'],0,3).'-'.substr($_POST['phone'],3,3).'-'.substr($_POST['phone'],6,4); } else { echo "Invalid phone number"; } } else { echo "User didn't provide phone number"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload