How to Provide Additional Phone Field Validation

Would you like to provide additional phone field validation? For example, if you’d like to prevent users from entering toll-free numbers in your Phone field, you can easily achieve this with a small PHP snippet. In this tutorial, we’ll walk you through the steps on how to prevent toll-free numbers from being entered in your form.

Creating the form

To begin, you’ll need to create your form and add your Phone field to your form as well as any additional fields you’ll need on your form.

If you need any help in creating your form, please see this documentation.

to provide additional phone field validation you will first need to create your form.

Setting the Phone field format

For the purpose of this documentation, we’re going to prevent any phone number starting with 8 or +8. Depending on which Format you’re using for the Phone form field will depend on the code snippet you’ll need to use below.

Select the format you want to use on your phone field

Additional Phone Field Validation

Now it’s time to add the PHP snippet you’ll need. There are two different snippets and this depends on what Format was set for the Phone field when you created your form.

If you need any help in adding snippets to your site, please see this tutorial.

US Phone Format

/**
 * Custom US format phone field Validation on submit (reject defined prefixes).
 *
 * @link  https://wpforms.com/developers/how-to-provide-additional-phone-field-validation/
 */

function wpf_dev_process_validate_phone($field_id, $field_submit, $form_data)
{
        //  Only check for US phone field format
	if ( 'us' === $form_data[ 'fields' ][$field_id][ 'format' ]) { 

		$stripped_phone = str_replace(array( '(', ')'), '', (string) $field_submit);

		$prefixes = [ '8', '+8' ];

		$regexp = implode( '|', $prefixes);

		if (preg_match( '/^[' . $regexp . ']/', $stripped_phone)) {
			wpforms()->process->errors[$form_data[ 'id' ]][$field_id] = esc_html__( 'The number should not start with "' . implode( '", "', $prefixes ) . '"', 'plugin-domain' );
		}
	}
}

add_action( 'wpforms_process_validate_phone', 'wpf_dev_process_validate_phone', 10, 3);

Smart and International Phone Format

/**
 * Custom Smartphone and International field Validation on submit (reject defined prefixes).
 *
 * @link  https://wpforms.com/developers/how-to-provide-additional-phone-field-validation/
 */

function wpf_dev_process_validate_phone( $field_id, $field_submit, $form_data ) {

   $prefixes = [ '8', '+8' ];

   $regexp = implode( '|', $prefixes );

   if ( preg_match( '/^[' . $regexp . ']/', (string) $field_submit ) ) {
      wpforms()->process->errors[ $form_data[ 'id' ] ][ $field_id ] = esc_html__( 'The number should not start with "' . implode( '", "', $prefixes) . '"', 'plugin-domain' );
   }
}

add_action( 'wpforms_process_validate_phone', 'wpf_dev_process_validate_phone', 10, 3 );

Both of these snippets will look at the Phone field value when the form is submitted and if it begins with the number 8 or +8 the form will not submit and an error message will appear.

By providing an additional phone field validation you can easily prevent toll-free numbers from being entered on your form.

And that’s it! You’ve successfully added additional validation for your Phone form field. Would you also like to prevent certain emails from being entered as well? Check out our tutorial on How to Restrict Email Domains.

Reference Filter

wpforms_process_validate_phone