How to Prevent Special Characters in Gravity Forms Validation?

Viewed 706

I am having an issue where users are inputting special characters such as á, é, ñ, etc (ex. Sánchez) into form fields and these special characters are conflicting with a different software we use. Our goal is to not allow these special characters on the specific gravity form using gform_field_validation() on the firstname and lastname fields but I am really struggling to write something that will actually work in the functions.php file. I have tried reviewing Gravity Forms' documentation but I have little to no experience with PHP and I am hoping someone here can possibly assist me...

Gravity Forms Documentation URL: https://docs.gravityforms.com/gform_field_validation/

Form ID = 2

First Name Field ID = 8

Last Name Field ID = 17

1 Answers

I´ve been looking for an answer to this too, and even after trying with gform_field_validation in some ways it did not work for me at all.

But I found out a workaround by registering a keypress event handler with jQuery and Javascript that fullfilled my needs:

    $('#YOUR-FIELD-ID-HERE').keypress(function (e) {
var txt = String.fromCharCode(e.which);
if (!txt.match(/[a-z0-9-]/)) {
    return false;
} });

Check out your field ID with inspector.

In this scenario, I just wanted to allow letters from a to z (not capitals), numbers from 0 to 9 and the character "-". Beware that this example does not allow blank spaces.

Reference: Disable symbols and non-letters in input field

Hope it helped!

Related