In a text field (name), can I enforce/validate one language only? [RESOLVED]

I’d like the user to enter English letters only, not alphabets in Cyrillic, Arabic, other languages, etc. Is this possible in Gravity Forms? It would only be for two fields on the form; the rest of the form is OK for user to enter other scripts/alphabets.

Thank you,
Jeremy

Hi Jeremy. This Stack Overflow discussion has some possible solutions:

It looks like you would find a snippet that works for you, and add it to the form in an HTML field, and target your input IDs. Let us know if you need more information. Thank you.

Thank you, Chris! We decided on a Javascript snippet.

A team member also said this: "of course before the input, there would be a form action in a code = “/action_page.php”

Where would I place the Javascript snippet/code? In a (hidden) Gravity Forms HTML Field? And how do I target the input IDs? I’ll have to do this for two fields on Form ID 3: Field ID 4 and Field ID 5.

Thank you so much for your guidance.

Yes, you can out the jQuery/JavaScript code into an HTML field you add to the form. It does not need to be hidden. The script won’t be visible on the page.

Share the code you plan on using, here, along with a link to the page where we can see the form, and we can help you with targeting the fields (normally it will be something like #input_22 as your jQuery selector.)

Thanks, Chris. Here’s a URL to see the form:
https://moreshetderech.com/product/membership-renewal-2021/

And here’s the code we would like to use for two of the fields on the page (שם פרטי באנגלית and שם משפחה באנגלית):

<input type="text" id="firstName"  onkeypress="return (event.charCode >= 65 && event.charCode <= 90) || (event.charCode >= 97 && event.charCode <= 122) || (event.charCode >= 48 && event.charCode <= 57)" />```

It looks like you want to add that JavaScript inline with the field? If so you can filter the markup when the form is rendered in order to add your JavaScript. You could use gform_field_content as the ‘time’ to add this JavaScript. You can use a PHP str_replace on something you know and replace with that thing you know, and your JavaScript. It would look something like this (this is for one field, field 43):

add_filter( 'gform_field_content_3_43', function ( $field_content, $field ) {
	return str_replace( "name='input_43'", "onkeypress='return (event.charCode >= 65 && event.charCode <= 90) || (event.charCode >= 97 && event.charCode <= 122) || (event.charCode >= 48 && event.charCode <= 57)' name='input_43'", $field_content );
}, 10, 2 );

You can duplicate that code and change the first line with the form and field ID and then the name attribute to apply to any other field in the form. If you have any other questions, please let us know.

Excellent, I’m following you (understanding). So that code you sent (and variations and different targeted fields) would be placed in… functions.php? Or where? And do you recommend a separate add_filter( gform_field_content for each target/field (easy for me to figure out and view/modify) or one combined add_filter( gform_field_content (better tidy code)?

thank you,
jeremy

That is PHP code which can be added to your theme functions.php file, or you can use a plugin such as this one to keep the code separate from the theme: Code Snippets – WordPress plugin | WordPress.org

And I think you will need to use each filter individually, because the form ID and field ID are in the filter name.

If you need any assistance, please let us know.

Thank you so much, Chris, everything you’ve told me works great. We’re using the Code Snippets plugin too. I’ll now adapt the code snippets to target different fields, and I think I’m comfortable using/changing HTML ASCII in the PHP to enforce different things (letters, numbers, special characters, etc).

Cheers,
Jeremy

1 Like