Phone number - prevent first digit from being 1 [RESOLVED]

Hello. I am seeing several form entries come through where the user errantly adds a 1 before their phone number. Incidentally, it cuts off the last digit of their phone number. Is there a way to prevent a 1 from ever being used as a first digit in the phone number field?

Thanks!

Hi Jeremy. You could use the gform_field_validation filter to ensure that the phone number never begins with a 1 and return an error if it does. Example #6 here shows using the gform_field_validation filter on a phone number field:

Your validation rule will be much simpler than the regex listed there (that is for UK phone validation.)

This code should do it. This will apply to a standard phone field in form 80:

// apply to form 80 only
add_filter( 'gform_field_validation_80', 'validate_phone_no_one', 10, 4 );
function validate_phone_no_one( $result, $value, $form, $field ) {
	if ( $field->type == 'phone' && $field->phoneFormat == 'standard' && $value[1] === '1' ) {
		$result['is_valid'] = false;
		$result['message']  = 'Please begin your phone number with area code.';
	}
	return $result;
}

@chrishajer That worked beautifully!! I can’t thank you enough. I was racking my brain for hours over this one. Much appreciated.

1 Like
© 2008 - 2021. Gravity Forms is a project by Rocketgenius Inc.