Validating a EAN-13 Code in a Numberfield

Hi,
this is a very specific question. If it is too specific for this forum, please feel free to tell me so.

I added a Contact Form 7 formular on the following site:
http://www.waschhalle-tuttlingen.de/kundenkarte-laden/

You see a field called “Kartennummer” where you should type a EAN-13 barcode. I would like to validate this barcode with its checksum. I know the code how to do the checking and I know that this probably would work with a WP-Filter. But I have no idea how to add such a filter and where.

Can anyone help? Probably just with a link to a tutorial.

Regards
Georg

function validate_EAN13Barcode($barcode)
{
    if (!preg_match("/^[0-9]{13}$/", $barcode)) {
        return false;
    }

    $digits = $barcode;

    $even_sum = $digits[1] + $digits[3] + $digits[5] +
                $digits[7] + $digits[9] + $digits[11];

    $even_sum_three = $even_sum * 3;

    $odd_sum = $digits[0] + $digits[2] + $digits[4] +
               $digits[6] + $digits[8] + $digits[10];

    $total_sum = $even_sum_three + $odd_sum;

    $next_ten = (ceil($total_sum / 10)) * 10;
    $check_digit = $next_ten - $total_sum;

    if ($check_digit == $digits[12]) {
        return true;
    }

    return false;
}

Hi there,

I am sure you will understand that this is completely a customization request and does not relate to our products. Unfortunately, we will not be able to implement the feature or maintain the code but we will do our best to give you the best suggestions we can.

Please kindly add the code below to X > Launch > Options > JS:

jQuery('.wpcf7-form').on('submit', function() {
	var theEAN = jQuery('input[name="EAN13"]');
	if (validate_EAN13Barcode(theEAN.val()) === false) {
		theEan.css('borderColor', 'red');
		return false;
	}
});

The code explained:

The first section of the code selects the form which you have added and adds an event handler to it on submit. So the code will be executed as soon as the form wants to get submitted.

The code itself searches for an input element with the name of EAN13 which is the input in question. The code gets the current value of the input and passes that to the function you provided regarding the validation.

The whole thing is inside an IF statement, so if the return value is false then the code makes the border of the input red to show there is a problem and returns false to avoid submission of the form.

I checked that on the link you gave us but there is an error regarding the preg_match section of the code you provided. Unfortunately, we cannot debug that portion of the code naturally.

Thank you for your understanding.

This sounds very much for the help I was looking for. I will have a closer look to is soon and will tell if it works. But at this point it sounds very easy to handle.

Regards and thanks a lot Christopher

You’re welcome.

Can you tell, where do I place the validate_EAN13Barcode()-Code?

We’re sorry but inquiries regarding the custom code and any fix and enhancements would be outside the scope of our support. For that, you need to seek help from a third party developer.

Thank you for understanding.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.