Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to give ERROR if the entered field value starts with 0, 00 or +?
    text
    copied!<p>On my site users must enter their phone number to get Verified and registered by SMS. In the function below users get ERROR:</p> <ol> <li>If the phone number is empty</li> <li>If the phone number exists (already registered)</li> <li>If the phone number is less then 10 digits</li> </ol> <p>Now I want to AUTO REMOVE/IGNORE if they have entered 00 or + before country code. (So if they enter 004798765432 or +4798765432 then the right value will be: 4798765432).</p> <p>And I want to show a DIFFERENT ERROR if the entered phone number starts with a SINGLE 0 (So if they enter 02298765432 then they will get ERROR msg like: Phone number you entered is not a international phone number).</p> <p>Here is the code that I'm using:</p> <pre><code>function registration_errors( $errors ) { $enabled = csnetworks_sms_get_option( 'register_form' ); if ( $enabled == 'on' ) { $phone = $_POST['user_phone']; if ( $phone == '' ) { $errors-&gt;add( 'empty_phone', __( '&lt;strong&gt;ERROR&lt;/strong&gt;: Please type your phone number.', 'csnetworks' ) ); } else if ( phone_exists( $phone ) ) { $errors-&gt;add( 'phone_exists', __( '&lt;strong&gt;ERROR&lt;/strong&gt;: Phone number is already registered.', 'csnetworks' ) ); } else { if ( preg_match( '/[^\d]/', $phone ) || (strlen( $phone ) &lt; 10 ) ) { $errors-&gt;add( 'invalid_phone', __( '&lt;strong&gt;ERROR&lt;/strong&gt;: Please type a valid phone number (10 digit min.)', 'csnetworks' ) ); } } } return $errors; } </code></pre> <p>/** Finally this solution worked for me */</p> <pre><code>/** * Validates phone number * * @param type $errors * @return type */ function registration_errors( $errors ) { $enabled = csnetworks_sms_get_option( 'register_form' ); if ( $enabled == 'on' ) { $phone = $_POST['user_phone']; </code></pre> <p>if ($phone[1] == '0') $phone = substr($phone, 2);</p> <p>if ($phone[0] == '0') { $errors->add( 'invalid_inter_phone', __( '<strong>ERROR</strong>: Phone Number you entered is NOT a international number.', 'csnetworks' ) ); } if ( $phone == '' ) { $errors->add( 'empty_phone', __( '<strong>ERROR</strong>: Please type your phone number.', 'csnetworks' ) ); } else if ( phone_exists( $phone ) ) { $errors->add( 'phone_exists', __( '<strong>ERROR</strong>: Phone number is already registered.', 'csnetworks' ) ); } else { if ( preg_match( '/[^\d]/', $phone ) || (strlen( $phone ) &lt; 10 ) ) { $errors->add( 'invalid_phone', __( '<strong>ERROR</strong>: Please type a valid Phone Number<br />(10 digit minimum and do NOT use "+")', 'csnetworks' ) ); }</p> <pre><code> } } return $errors; } </code></pre>
 

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