Title: Custom Form Validation
Last modified: July 27, 2020

---

# Custom Form Validation

 *  Resolved [Lord_iMac](https://wordpress.org/support/users/lord_imac/)
 * (@lord_imac)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/custom-form-validation-2/)
 * Hello, a few years ago someone added code to our website, which validates a field
   against a prefix and field length.
 * The field validation code looks like this:
 *     ```
       add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
   
       function my_custom_checkout_field_process() {
       	if ((substr($_POST['billing_manr'],0,3) <> "MA-") || (strlen($_POST['billing_manr']) <> 9))
       	wc_add_notice( __( 'Error code ...' ), 'error' );
       }
       ```
   
 * It validates against MA- and an allover length of 9 letters. For example, MA-
   123123 is OK, MA-123 isn’t.
 * Now we want to validate against MA-xxxxxx and BW-xxxxxx but I’m not sure how 
   the correct syntax is.
 * I tried already the following:
 *     ```
       add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
   
       function my_custom_checkout_field_process() {
       	if ((substr($_POST['billing_manr'],0,3) <> "MA-") || (substr($_POST['billing_manr'],0,3) <> "BW-") || (strlen($_POST['billing_manr']) <> 9))
       	wc_add_notice( __( 'Error code ...' ), 'error' );
       }
       ```
   
 * But with this the validation fails again both, MA- and BW-.
 * Can someone help me with this? Thanks and best regards!
    -  This topic was modified 5 years, 10 months ago by [Lord_iMac](https://wordpress.org/support/users/lord_imac/).
    -  This topic was modified 5 years, 10 months ago by [Lord_iMac](https://wordpress.org/support/users/lord_imac/).

Viewing 1 replies (of 1 total)

 *  Thread Starter [Lord_iMac](https://wordpress.org/support/users/lord_imac/)
 * (@lord_imac)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/custom-form-validation-2/#post-13175847)
 * Solved, thanks to stackoverflow.
 *     ```
       add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
       function my_custom_checkout_field_process() {
           if ( isset($_POST['billing_manr']) 
           && ( ! in_array( substr($_POST['billing_manr'], 0, 3), array('MA-', 'BW-') ) 
           || strlen($_POST['billing_manr']) <> 9 ) ) {
               wc_add_notice( __( 'Error code ...', 'woocommerce' ), 'error' );
           }
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Custom Form Validation’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

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

 * 1 reply
 * 1 participant
 * Last reply from: [Lord_iMac](https://wordpress.org/support/users/lord_imac/)
 * Last activity: [5 years, 10 months ago](https://wordpress.org/support/topic/custom-form-validation-2/#post-13175847)
 * Status: resolved