Contact Form 7 Validate Minlength - php

first of all I'm using latest WordPress and CF7 version. I want to include the minlength validation for the tel field before. I know the syntax minlength=""
can be used inside the CF7, but for unknown reason, it won't work. Only maxlength="" is ok.
I already contacted the plugin support, but seems like no further response. So, I search here and found some code and I edit it so that the field will return an error if user put less than 10 characters. I put the codes inside functions.php
function custom_phone_validation($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if($name == 'Subject'){
$phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
if($phoneNumber < "9"){
$result->invalidate( $tag, "phone number is less" );
}
}
return $result;
}
add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);
The result now is, it always display the "phone number is less" even though I insert more than 9 characters. May I know what wrong and how to solve it?

as I've tested you must have your tel field [tel* phonenumber tel-503] where phonenumber is name of postfield you are posting, second issue in your code is $name=='Subject' as you are validating tel so $name will be phonenumber. So it will be like this:
function custom_phone_validation($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if($name == 'phonenumber'){
$phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
if(strlen($phoneNumber) < 9){
$result->invalidate( $tag, "phone number is less" );
}
}
return $result;
}
add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);

Your $phoneNumber is a string. You will need to get the length of the string to compare with 9.
Your code will become:
function custom_phone_validation($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if($name == 'Subject'){
$phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
if(strlen($phoneNumber) < 9){//<=====check here
$result->invalidate( $tag, "phone number is less" );
}
}
return $result;
}
add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);

Related

Php postcode validator for partial UK postcode

I am working on a project that allows a user to search shops close to their postcode. As the site is now, it verifies via a full postcode only such as M28 3HE. I need to amends this to allow the user to be able to search for partial postcodes such as M28. Looking through the files, this looks to be the validator:
private function is_valid_postcode( $postcode ) {
// Remove all whitespace.
$postcode = preg_replace( '/\s/', '', $postcode );
$postcode = strtoupper( $postcode );
if ( preg_match( '/^[A-Z]{1,2}[0-9]{2}$/', $postcode )
|| preg_match( '/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/', $postcode )
|| preg_match( '/^GIR0[A-Z]{2}$/', $postcode ) ) {
return true;
} else {
$_SESSION['shop_errors'][] = 'The postcode entered could not be validated.';
return false;
}
}
How can I amend this to allow for partial postcodes?
Thank you

Wordpress Contact Form 7 custom validation rule not executed

Can't seem to get add_filter() to get working to add a custom validation rule to contact form 7 (v5.2.1). Tried many examples that claim to work from this website and other webs. But non seem to work so far. Can anyone shed some light on this please.
Tried below code.
function custom_text_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'your-subject') {
$value = $_POST[$name];
if (preg_match('/[\'^£$%&*()}{#~><>|=_+¬]/', $value)){
// $result->invalidate( $tag, "Invalid characters." ); // this did not work
$result['valid'] = false;
$result['reason'][$name] = 'Invalid characters';
}
}
return $result;
}
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 999, 2);
Since above code does not work, tried following.
function custom_text_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'your-subject') {
$value = $_POST[$name];
if (preg_match('/[\'^£$%&*()}{#~><>|=_+¬]/', $value)){
// $result->invalidate( $tag, "Invalid characters." ); // this did not work
$result['valid'] = false;
$result['reason'][$name] = 'Invalid characters';
}
}
//Added below lines to fire validation error no matter what. But still contact form submits successfully.
$result['valid'] = false;
$result['reason'][$name] = 'Invalid characters';
return $result;
}
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 999, 2);
Further tried different priorities to add_filter too.
add_filter('wpcf7_validate_text','custom_text_validation_filter', 1, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 2, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 10, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 20, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2);
Non worked
Are you doing well in your function?
The parameter $tag is an object, it seems to me.
Other than that, I don't see what is blocking :/
https://contactform7.com/2015/03/28/custom-validation/

can't target gravityform input or field ID in filter

I have a filter/validation script that only accepts emails from a certain domain. This is working pretty good but I need to have it on a specific from input ID:
As per https://docs.gravityforms.com/gform_field_validation/#11-email-validation-by-third-party-api#usage :
You can also target a specific field by adding the form id and field
id after the hook name.
//The following declaration targets field 1 in form 6 add_filter(
'gform_field_validation_6_1', 'your_function_name', 10, 4 );
Currently my code looks like this:
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
if ( ! $result['is_valid'] && $field->get_input_type() === 'email' && strpos($field->get_field_input(), 'company.com') === true) {
$result['is_valid'] = true;
$result['message'] = '';
} else {
$result['is_valid'] = false;
$result['message'] = 'E-Mail must be a company.com address';
}
return $result;
}, 10, 4 );
My form and input IDs are confirmed as 1 and 13, respectively:
When I change the code to the following, the script stops working:
add_filter( 'gform_field_validation_13_1', function ( $result, $value, $form, $field ) {
if ( ! $result['is_valid'] && $field->get_input_type() === 'email' && strpos($field->get_field_input(), 'company.com') === true) {
$result['is_valid'] = true;
$result['message'] = '';
} else {
$result['is_valid'] = false;
$result['message'] = 'E-Mail must be a company.com address';
}
return $result;
}, 10, 4 );
Is there another way I need to check my form ID? What is missing here?
From the documentation gform_field_validation_6_1 means that it targets field 1 in form 6.
You form id is 1 and input field id is 13 so the filter should be gform_field_validation_1_13.

Check for mobile number CF7

I have a ContactForm7 form which submits data to a CRM. All works fine but now I need to differentiate between mobile and landline numbers. If a number starts with 07 it will be accepted as a mobile number.
looking at other threads I've tried the following but now neither the mobile or telephone field are being populated in the crm or being passed to the log file?
function process_contact_form_data( $contact_form ) {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
if ( 'Quote Form_Contact' || 'Quote Form_Product' || 'Quote Form' == $title ) {
$firstName = $posted_data['user_first_name'];
$lastName = $posted_data['user_last_name'];
$email= $posted_data['your-email'];
$phone = $posted_data['your-number'];
$message = $posted_data['your-message'];
$bp = $posted_data['BP'][0];
$phone = $pattern;
$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";
$match = preg_match($pattern,$phone);
if ($match != false) {$mobile = $phone;} else {$mobile= '';};
}
$error = false;
try
{
$relationshipId = postRelationship($firstName,$lastName,$email,$phone,$bp);
$opportunityId = postOpportunity($relationshipId,$message);
postOpportunityNote($relationshipId,$opportunityId,$message);
// postTask($relationshipId);
}
catch (Exception $e)
{
$error=true;
}
if($error || !isset($relationshipId) || !isset($opportunityId) || $relationshipId <= 0 || $opportunityId <= 0)
{
$log->lfile(ABSPATH . 'quotevine.log');
$log->lwrite('ERROR: With Email Address ' . $email);
$log->lclose();
}
}
add_action( 'wpcf7_before_send_mail', 'process_contact_form_data');
Your are overwriting your $phone variable in this line $phone = $pattern; with an undefined variable which will cause $phone to be NULL.
But after commenting out that line, the value of $mobile will still not be correct because a mobile number starts with 07 and the regex matches both the landline and the mobile number, for example:
07123123123
+447123123123
What you could do is if the match succeeded, check if the string starts with +44 to verify it is a mobile number.
preg_match returns false if an error occurred but I think you want to verify if the match is correct.
$phone = "+447123123123";
$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";
$mobile= '';
if (preg_match($pattern,$phone) && 0 === strpos($phone, '07')) {
$mobile = $phone;
}
You overwrite the phone with an undefined variable.
// Phone is now a phone number I assume
$phone = $posted_data['your-number'];
$message = $posted_data['your-message'];
$bp = $posted_data['BP'][0];
// $pattern is as far as I can see undefined
// $phone =NULL
$phone = $pattern;
// You set pattern
$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";
// Here you regex if the pattern matches NULL which it does not.
$match = preg_match($pattern,$phone);

Validate phone number in php

I am making a phone number validator and all numbers should start with 168 and then followed by 9 digits 123456789.In total the phone number should only have 12 digits.
I have tried this
$phoneNumber = '168921500789';
if( preg_match( "/^\168[0-9]{9}$/", $phoneNumber ) ){
echo "Valid number";
} else {
echo "Invalid number";
}
When i run the script,the number is not valid.How can i make the script take only 12 digits that must start with 168?.
Your RegEx is wrong, there's an useless backslash before the 1. Correct:
/^168[0-9]{9}$/
Full code:
$phoneNumber = '168921500789';
if( preg_match( "/^168[0-9]{9}$/", $phoneNumber ) ){
echo "Valid number";
} else {
echo "Invalid number";
}
You had to remove 1 slash:
$phoneNumber = '168921500789';
if( preg_match( "/^168[0-9]{9}$/", $phoneNumber ) ){
echo "Valid number";
} else {
echo "Invalid number";
}
we using like this, with referred this link
public function validatePhoneNumber($number){
$formats = array(
'###-###-####', '####-###-###',
'(###) ###-###','####-####-####',
'##-###-####-####','####-####','###-###-###',
'#####-###-###', '##########', '#########',
'# ### #####', '#-### #####'
);
$format = trim(preg_replace('/[0-9]/', '#', $number));
return (in_array($format, $formats)) ? true : false;
}

Categories