Jump to content

helenaqurfer

Members
  • Posts

    5
  • Joined

  • Last visited

helenaqurfer's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi Psycho, Thank you, that makes sense. I think this method is much more clear than what I was attempting earlier. Thanks again!
  2. Hi Psycho, it's more of a formatting issue. I'm not as concerned about users entering fake numbers - it's more about apprising them if they have accidentally missed a digit, typed a letter, etc...as a courtesy and a validation that they have entered their actual number and have not made any mistakes. So what you are suggesting is to strip all non-numerical characters and then just test for length?
  3. Above the code previously posted is the field where the user inputs the phone number: //1. Add a new form element... add_action('register_form','myplugin_register_form2'); function myplugin_register_form2 (){ $phone_number = ( isset( $_POST['phone_number'] ) ) ? $_POST['phone_number']: ''; ?> <p id="phone_number"> <label for="phone_number"><?php _e('Phone Number <font size="1">(XXX XXX XXXX)</font>','mydomain') ?><br /> <input type="text" name="phone_number" id="phone_number" class="input" size="25" style="text-align:right" maxlength="14" /> </p> <?php } My impression is it should be pulling this value from what the user enters and then match that against the expression and generate the error if the number does not match the expression.
  4. Thank you. I noticed about the variables and also noticed that I didn't want to match against the array but the user input into the field. I changed it thusly but even if I type a correct format that seems to match the expression I am still getting an invalid number: //2. Add validation. In this case, we make sure phone_number is required. add_filter('registration_errors', 'myplugin_registration_errors2', 10, 3); function myplugin_registration_errors2 ($errors, $sanitized_user_login, $user_email) { $sPattern = "/^ (?: # Area Code (?: \( # Open Parentheses (?=\d{3}\)) # Lookahead. Only if we have 3 digits and a closing parentheses )? (\d{3}) # 3 Digit area code (?: (?<=\(\d{3}) # Closing Parentheses. Lookbehind. \) # Only if we have an open parentheses and 3 digits )? [\s.\/-]? # Optional Space Delimeter )? (\d{3}) # 3 Digits [\s\.\/-]? # Optional Space Delimeter (\d{4})\s? # 4 Digits and an Optional following Space (?: # Extension (?: # Lets look for some variation of 'extension' (?: (?:e|x|ex|ext)\.? # First, abbreviations, with an optional following period | extension # Now just the whole word ) \s? # Optionsal Following Space ) (?=\d+) # This is the Lookahead. Only accept that previous section IF it's followed by some digits. (\d+) # Now grab the actual digits (the lookahead doesn't grab them) )? # The Extension is Optional $/x"; if (!preg_match($sPattern, $phone_number, $aMatches)) { $errors->add( 'phone_number_error', __('<strong>ERROR</strong>: You must include a valid phone number.','mydomain') ); return $errors; } if ( empty( $_POST['phone_number'] ) ) $errors->add( 'phone_number_error', __('<strong>ERROR</strong>: You must include a valid phone number.','mydomain') ); return $errors; } //3. Finally, save our extra registration user meta. add_action('user_register', 'myplugin_user_register2'); function myplugin_user_register2 ($user_id) { if ( isset( $_POST['phone_number'] ) ) update_user_meta($user_id, 'phone_number', $_POST['phone_number']); }
  5. I'm trying to create a field that will validation US phone numbers, and afterwards I will be attempting a field to validate income. So far, for the phone numbers, I have implemented the following regex expression and PHP. The regex has worked in someone elses implementation, however when I utilize it in this implementation it always returns an error that the phone number isn't valid. I'm having a difficult time seeing where the difficulty is: //1. Add a new form element... add_action('register_form','myplugin_register_form2'); function myplugin_register_form2 (){ $phone_number = ( isset( $_POST['phone_number'] ) ) ? $_POST['phone_number']: ''; ?> <p id="phone_number"> <label for="phone_number"><?php _e('Phone Number <font size="1">(XXX XXX XXXX)</font>','mydomain') ?><br /> <input type="text" name="phone_number" id="phone_number" class="input" size="25" style="text-align:right" maxlength="14" /> </p> <?php } //2. Add validation. In this case, we make sure phone_number is required. add_filter('registration_errors', 'myplugin_registration_errors2', 10, 3); function myplugin_registration_errors2 ($errors, $sanitized_user_login, $user_email) { $sPattern = "/^ (?: # Area Code (?: \( # Open Parentheses (?=\d{3}\)) # Lookahead. Only if we have 3 digits and a closing parentheses )? (\d{3}) # 3 Digit area code (?: (?<=\(\d{3}) # Closing Parentheses. Lookbehind. \) # Only if we have an open parentheses and 3 digits )? [\s.\/-]? # Optional Space Delimeter )? (\d{3}) # 3 Digits [\s\.\/-]? # Optional Space Delimeter (\d{4})\s? # 4 Digits and an Optional following Space (?: # Extension (?: # Lets look for some variation of 'extension' (?: (?:e|x|ex|ext)\.? # First, abbreviations, with an optional following period | extension # Now just the whole word ) \s? # Optionsal Following Space ) (?=\d+) # This is the Lookahead. Only accept that previous section IF it's followed by some digits. (\d+) # Now grab the actual digits (the lookahead doesn't grab them) )? # The Extension is Optional $/x"; // /x modifier allows the expanded and commented regex $aNumbers = array( '123-456-7890x123', '123.456.7890x123', '123 456 7890 x123', '(123) 456-7890 x123', '123.456.7890x.123', '123.456.7890 ext. 123', '123.456.7890 extension 123456', '123 456 7890', '123-456-7890ex123', '123.456.7890 ex123', '123 456 7890 ext123', '456-7890', '456 7890', '456 7890 x123', '1234567890', '() 456 7890' ); foreach($aNumbers as $sNumber) { if (!preg_match($sPattern, $phone_number, $aMatches)) { $errors->add( 'phone_number_error', __('<strong>ERROR</strong>: You must include a valid phone number.','mydomain') ); return $errors; } } if ( empty( $_POST['phone_number'] ) ) $errors->add( 'phone_number_error', __('<strong>ERROR</strong>: You must include a valid phone number.','mydomain') ); return $errors; } //3. Finally, save our extra registration user meta. add_action('user_register', 'myplugin_user_register2'); function myplugin_user_register2 ($user_id) { if ( isset( $_POST['phone_number'] ) ) update_user_meta($user_id, 'phone_number', $_POST['phone_number']); }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.