Skip Navigation

[Resolved] How to validate number field (10 digits, starting with '06') using preg_match

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.15 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 3 months ago.

Assisted by: Minesh.

Author
Posts
#2717589

Tell us what you are trying to do?
I am trying to validate a post form field 'guest_phone' to have 10 digits and starting with '06'

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Is there a similar example that we can see?

I am using the function below:


function email_domain_validation($email){
  if (!preg_match('/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]{2,})(\.[a-z]{2,4})$/i', $email)) return false;
  
  $domains = array('mail.ru','op.ru','mail.ro','op.ro','mail.pl','op.pl');
  list(, $email_domain) = explode('@', $email, 2);
  return in_array($email_domain, $domains);
}

add_filter('cred_form_validate','func_email_validation',10,2);
function func_email_validation($error_fields, $form_data){
    list($fields,$errors)=$error_fields;
     
     if ($form_data['id']==791){ // id of email post-form
   
           $is_unvalid_email_domain =  email_domain_validation($_POST['wpcf-guest_email']);
           if($is_unvalid_email_domain)  {
                $errors['wpcf-guest_email']='Dit e-mailadres is niet toegestaan.';
           }
           //check hidden_field value
           if ($fields['wpcf-hidden_field']['value']!='') {
               //set error message for hidden_field
               $errors['hidden_field']='We hebben je gevangen, jij stiekemerd!';
           }
           // check guest_phone has 10 digits and starting with 06
           if ($fields['wpcf-guest_phone']['value']!=preg_match('/^(?:06)\d{10}$') ) {
               //set error message for guest_phone
               $errors['guest_phone']='Geef een correct mobiel nummer';
           }  
     }
    return array($fields,$errors);
}

I would like to get assistance in making this part below with 'preg_match' work.

           // check guest_phone has 10 digits and starting with 06
           if ($fields['wpcf-guest_phone']['value']!=preg_match('/^(?:06)\d{10}$') ) {
               //set error message for guest_phone
               $errors['guest_phone']='Geef een correct mobiel nummer';
           } 
#2717717

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You should try to use the following code:

$pattern = "/^(06)[0-9]{8}$/";  
$is_guest_validate = preg_match($pattern, $fields['wpcf-guest_phone']['value']);

if (!$is_guest_validate) {
    //set error message for guest_phone
    $errors['guest_phone']='Geef een correct mobiel nummer';
}