Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Require a phone number with at least 5 numbers, dashes, spaces, periods or parentheses.
<?php
/**
* Plugin Name: Simple Phone Validation for Restaurant Reservations
* Plugin URI: http://themeofthecrop.com
* Description: Check if a reservations's phone number is not empty and includes only numbers, spaces, dashes, periods and parentheses.
* Version: 1.0
* Author: Theme of the Crop
* Author URI: http://themeofthecrop.com
* License: GNU General Public License v2.0 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Requires at least: 4.0
* Tested up to: 4.1
*
* Text Domain: rtbdomain
* Domain Path: /languages/
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Check if a reservations's phone number is not empty and includes only numbers, spaces, dashes, periods and parentheses.
*/
if ( !function_exists( 'rtbspv_validate_phone' ) ) {
add_action( 'rtb_validate_booking_submission', 'rtbspv_validate_phone' );
function rtbspv_validate_phone( $booking ) {
// Check if it's empty
if ( empty( $booking->phone ) ) {
$booking->validation_errors[] = array(
'field' => 'phone',
'post_variable' => $_POST['rtb-phone'],
'message' => __( 'Please enter a phone number where we can reach you.', 'restaurant-reservations' ),
);
return;
}
// Check for between 5 and 50 valid characters
// valid: space, -, 0-9, ., (, )
preg_match( '/(\d?[\s-()\.]*){5,50}\d/', $booking->phone, $matches );
if ( empty( $matches ) ) {
$booking->validation_errors[] = array(
'field' => 'phone',
'post_variable' => $_POST['rtb-phone'],
'message' => __( 'Please enter a valid phone number where we can reach you.', 'restaurant-reservations' ),
);
}
}
} // endif
@crysttn

This comment has been minimized.

Copy link

@crysttn crysttn commented Feb 3, 2015

Hey Nate, may I ask how do I install this micro plugin for Restaurant Reservations?

@NateWr

This comment has been minimized.

Copy link
Owner Author

@NateWr NateWr commented Feb 25, 2015

HI @tjoengnguyen, sorry I'm just seeing this email now. To install it, click the Download Gist link on the right. Upload the .php file to your /wp-content/plugins directory. Then browse to your Plugins list in your WordPress admin panel and activate the Simple Phone Validation for Restaurant Reservations plugins.

@jseutens

This comment has been minimized.

Copy link

@jseutens jseutens commented Aug 28, 2016

Nate,
For the translation with loco translator in wordpress.
Loco can't find the 2 messages , the Text Domain: rtbdomain is found but the messages use 'restaurant-reservations' , if i changed this to rtbdomain it works with the translate string.
Is that a good idea or will it fail further up the line when posting a booking ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment