Replies: 0
Hi, Takayuki.
I have created a form and applied a mask to phone field using the Masked Input plugin.
However, I would like to do the following validation:
1. If the user not fill the entire field, the following message should be displayed: “Required Field”.
2. If the user fill partially the field, the following message should be displayed: “Invalid Phone Number”.
I have created a function as a filter to test the field, but the impression is that it assumes that the field is always empty. Therefore I can’t validate the field as I would like.
But if I disable the mask, my function works correctly.
Any sugestions?
Follow the code.
`function validate_cell_phone($result, $tag){
$tag = new WPCF7_FormTag ($tag);
if(‘cellphone’ == $tag->name) {
$cellphone = isset( $_POST[$tag->name] ) ? trim( $_POST[$tag->name] ) : ”;
$cellphone = preg_replace(‘/[^0-9]/’, ”, (string) $cellphone);
$value = preg_match(“/^[(](\d{2})[)]\s(\d{1})\s(\d{4})[-](\d{4})$/”, $_POST[$tag->name]);
if ($cellphone == “”) {
$result->invalidate ($tag, “Required Field”);
}
elseif($value == false ) {
$result->invalidate( $tag, “Invalid Phone Number” );
}
}
return $result;
}
add_filter(‘wpcf7_validate_tel’, ‘validate_cell_phone’, 10, 2);
add_filter(‘wpcf7_validate_tel*’, ‘validate_cell_phone’, 10, 2);
Thank you.