• Resolved Lord_iMac

    (@lord_imac)


    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 4 years, 7 months ago by Lord_iMac.
    • This topic was modified 4 years, 7 months ago by Lord_iMac.
Viewing 1 replies (of 1 total)
  • Thread Starter Lord_iMac

    (@lord_imac)

    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.