Title: Regex method (Important)
Last modified: June 18, 2024

---

# Regex method (Important)

 *  Resolved [Yash Kumar](https://wordpress.org/support/users/yashkumar4443/)
 * (@yashkumar4443)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/regex-method-important/)
 * Suppose I have created name field, in which i want to implement regex method 
   for letters only, No numeric value should be passed on submission
   It shoukd immediately
   show an error to the user.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Support [Laura – WPMU DEV Support](https://wordpress.org/support/users/wpmudev-support8/)
 * (@wpmudev-support8)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/regex-method-important/#post-17832489)
 * Hi [@yashkumar4443](https://wordpress.org/support/users/yashkumar4443/)
 * I hope you’re well today!
 * You can use code like this for custom validation:
 *     ```
       add_filter( 'forminator_custom_form_submit_errors', 'forminator_custom_validate_field', 99, 3 );
       function forminator_custom_validate_field( $submit_errors, $form_id, $field_data_array ) {
   
   
       	$form_ids = array( 3010 ); // IDs of forms to check; if more than one, separate by comma 
       	$fields = array( 'name-1' ); // list of fields to validate
       	$error_msg = 'Invalid field'; // custom error message
   
   
   
       	if ( in_array( $form_id, $form_ids ) ) {
   
       		foreach( $field_data_array as $arr ) {
       			if ( in_array( $arr['name'], $fields ) ) {
   
       				// YOUR VALIDATION CODE GOES HERE
       				// THE VALUE THAT YOU WANT TO CHECK is in $arr['value'];
   
       				// example below will issue form error if field contains string TEXT
       				if ( strpos( $arr['value'], 'TEXT' ) !== false ) {
   
       					// issue error 
       					$submit_errors[][$arr['name']] = $error_msg;
       				}
       			}
   
       		}
   
       	}
   
       	return $submit_errors;
       }
       ```
   
 * You would need to set your form ID, field IDs and error message and put your 
   own validation code inside (see comments in code) and it will do the trick, showing
   general error above the form and your custom error directly below the validated
   field.
 * Kind regards,
    Adam
 *  Plugin Support [Laura – WPMU DEV Support](https://wordpress.org/support/users/wpmudev-support8/)
 * (@wpmudev-support8)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/regex-method-important/#post-17848829)
 * Hi [@yashkumar4443](https://wordpress.org/support/users/yashkumar4443/)
 * We didn’t hear back from you for quite some time already so I hope that suggested
   solution worked for you.
 * I’m marking this topic as resolved then but if you have any follow-up questions,
   don’t hesitate to ask.
 * Best regards,
    Adam
 *  Thread Starter [Yash Kumar](https://wordpress.org/support/users/yashkumar4443/)
 * (@yashkumar4443)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/regex-method-important/#post-17938675)
 * Ohh, Hi there, Sorry I missed the notifications, Thanks much for your help, But
   still it isn’t works the way I want, Actually i am having two fields, name and
   email in one form, and any user is passing anything in both the fields.
   for e.
   g below – Name – jhhggfhfh(or number 786876886687)
 * Email – [nfhgffytfft@jgjgjhg.com](https://wordpress.org/support/topic/regex-method-important/nfhgffytfft@jgjgjhg.com?output_format=md)
   
   I need help to fix this….Please help, below is my code, I have modified –************************************************************
 *     ```wp-block-code
       add_filter( 'forminator_custom_form_submit_errors', 'forminator_custom_validate_field', 99, 3 );function forminator_custom_validate_field( $submit_errors, $form_id, $field_data_array ) {    // Define the form ID to check    $form_ids = array( 13152 ); // Form ID(s) to check    // Define the fields to validate    $fields = array( 'name-1', 'email-1' ); // List of fields to validate    // Define the error message    $error_msg = 'Invalid input'; // Custom error message    // Check if the current form is in the list of form IDs    if ( in_array( $form_id, $form_ids ) ) {        foreach( $field_data_array as $arr ) {            if ( in_array( $arr['name'], $fields ) ) {                // YOUR VALIDATION CODE GOES HERE                // The value that you want to check is in $arr['value'];                                // Example validation: Check if field 'name-1' contains the string 'TEXT'                if ($arr['name'] === 'name-1' && strpos( $arr['value'], 'TEXT' ) !== false) {                    // Issue error if validation fails                    $submit_errors[][$arr['name']] = $error_msg;                }                // Example validation: Check if email field 'email-1' is not a valid email address                if ($arr['name'] === 'email-1' && !filter_var($arr['value'], FILTER_VALIDATE_EMAIL)) {                    // Issue error if validation fails                    $submit_errors[][$arr['name']] = 'Invalid email address';                }            }        }    }    return $submit_errors;}
       ```
   
 *  Plugin Support [Nithin – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport11/)
 * (@wpmudevsupport11)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/regex-method-important/#post-17941231)
 * Hi [@yashkumar4443](https://wordpress.org/support/users/yashkumar4443/),
 * There isn’t any way to validate based on what users manually type other than 
   setting rules on what could be entered ie for example not allowing numbers for
   the Name field and only allowing valid domains for email ie only from Gmail, 
   outlook etc
 *     ```wp-block-code
       <?phpadd_filter( 'forminator_custom_form_submit_errors', 'forminator_custom_validate_field', 99, 3 );function forminator_custom_validate_field( $submit_errors, $form_id, $field_data_array ) {        $form_ids = array( 1324 ); // IDs of forms to check; if more than one, separate by comma     $fields_to_validate = array( 'name-1', 'email-1' ); // fields to validate    $error_msg_name = 'Please enter a name with letters only.';     $error_msg_email = 'Please use a valid email';        if ( in_array( $form_id, $form_ids ) ) {                foreach( $field_data_array as $arr ) {            if ( in_array( $arr['name'], $fields_to_validate ) ) {                                // Validation for name field                if ( $arr['name'] == 'name-1' && !preg_match('/^[a-zA-Z\s]+$/', $arr['value']) ) {                    $submit_errors[][$arr['name']] = $error_msg_name;                }                                // Validation for email field                 if ( $arr['name'] == 'email-1' && !preg_match('/@(gmail\.com|outlook\.com)$/', $arr['value']) ) {                    $submit_errors[][$arr['name']] = $error_msg_email;                }            }        }    }    return $submit_errors;}
       ```
   
 * You can refer the above example snippet, which restricts Name field if number
   is entered and only allows email field with gmail or outlook email address.
 * You can use the above as a reference and tweak it according to your needs.
 * You can implement the above code using mu-plugins. Please check this link on 
   how to implement the above code as a mu-plugins:
   [https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins](https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins)
 * Kind Regards,
 * Nithin

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Regex method (Important)’ is closed to new replies.

 * ![](https://ps.w.org/forminator/assets/icon-256x256.gif?rev=3443182)
 * [Forminator Forms – Contact Form, Payment Form & Custom Form Builder](https://wordpress.org/plugins/forminator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/forminator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/forminator/)
 * [Active Topics](https://wordpress.org/support/plugin/forminator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/forminator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/forminator/reviews/)

## Tags

 * [regex](https://wordpress.org/support/topic-tag/regex/)

 * 4 replies
 * 3 participants
 * Last reply from: [Nithin – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport11/)
 * Last activity: [1 year, 8 months ago](https://wordpress.org/support/topic/regex-method-important/#post-17941231)
 * Status: resolved