Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Alphanumeric Validation Contact Form 7 WordPress

  • SOLVED

How do i get Contact Form 7 to validate a field with only alphanumeric entries?

Thanks.

Answers (4)

2016-11-20

Reigel Gallarde answers:

something like this..

add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );

function alphanumeric_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );

if ( 'name-of-the-input' == $tag->name ) {
$name_of_the_input = isset( $_POST['name-of-the-input'] ) ? trim( $_POST['name-of-the-input'] ) : '';

if ( !preg_match('/^[a-zA-Z0-9]+$/',$name_of_the_input) ) {
$result->invalidate( $tag, "Allowed characters are alphanumeric only" );
}
}

return $result;
}


this assumes that you are validating something like: [text* name-of-the-input]


aaronwp comments:

Hello,

Could you provide a guide to how that would work in visual composer and where I would need to paste the code and etc?

Basically a step by step guide.

Non coder here.

Thanks.


Reigel Gallarde comments:

you have to paste this code in functions.php file of the current theme... you need to edit this file..


aaronwp comments:

Hello Reigel.

Tried putting it in the function.php file but it doesnt seem to work.


Reigel Gallarde comments:

what is your cf7 code/tags?


aaronwp comments:

This one.

[text* name-of-the-input]


Reigel Gallarde comments:

I've check the code and it's working... demo link http://demo.reigelgallarde.me/ppqp/sample-page/


aaronwp comments:

Hello Reigel,

Thanks for this.
However, when I key in only letters, the form is still valid.

Perhaps I wasn't clear on the information but the field MUST contain letters and numbers and not ONLY letters and numbers.


Reigel Gallarde comments:

if that's the case, you need to add one more check... your new code would be like this:

add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );

function alphanumeric_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );

if ( 'alpha' == $tag->name ) {
$name_of_the_input = isset( $_POST['alpha'] ) ? trim( $_POST['alpha'] ) : '';

if ( !preg_match('/^[a-zA-Z0-9]+$/',$name_of_the_input) ) {
$result->invalidate( $tag, "Allowed characters are alphanumeric only" );
} elseif (!(preg_match('/[A-Za-z]/', $name_of_the_input) && preg_match('/[0-9]/', $name_of_the_input))) {
$result->invalidate( $tag, "Must contain at least one letter and one number!" );
}
}

return $result;
}


please check this sample: http://demo.reigelgallarde.me/ppqp/sample-page/

2016-11-20

Duncan O'Neill answers:

OR you could try it the easier way. In your wp backend ( at yourdomain.com/wp-admin/ ), go to Contact -> Contact Forms. A list of forms on the site should appear. Hover over the one you want to edit. Click on the edit link which appears.

In the field you want to be numerical, enter something like the following:

[number* your-number min:6 max:12]

That says that the field must be numerical, and that between 6 and 12 digits should be entered. The asterisk ( star ) means it's a required field.

Don't forget the SAVE the form.

More information here;
http://contactform7.com/number-fields/

hope this helps.


aaronwp comments:

Hello Duncan,

Thanks for the reply.

I know how to validate for digits only or letters only.

The question here is that, I need the validation to be only numbers or letters.

I hope this clarifies things.

2016-11-21

Arnav Joy answers:

are you still looking for help ??


aaronwp comments:

Yes please!

2016-11-23

Rempty answers:

Hello aaronwp
The code of Reigel works fine.

Try embed the contact form 7 via shortcode
[contact-form-7 id="1234" title="Contact form 1"]