How to restrict user typing more numbers than required?

By: Danier Valiev | Asked: 06/15/2023
ForumsCategory: General questionsHow to restrict user typing more numbers than required?
Danier Valiev asked 2 years ago
The question is how do i prevent users to continue typing numbers when I for example only need 4 digit number. They might get message at the end saying the number have to be less than 9999 but that is annoying. 

I have attached image. 
Attachments
4 Answers
Rob LeVineRob LeVine answered 2 years ago
I was thinking that you could add the maxlength attribute to the input field in the Customize HTML section of the form's settings, but apparently that's ignored for an input field of type "number". With just a few lines of jQuery you can accomplish what you need.
Danier Valiev replied 2 years ago

I did try to add this code to "Before field" inside Customize html:

script
jQuery(function($) {
// Set the maximum length for the number field
$('#field_ID').attr('maxlength', '4');
});
script

I changed field_ID to the actual field ID of the number input, but that did nothing.

Rob LeVineRob LeVine replied 2 years ago

That won't work because maxlength does not work with an input field of type number. You have to set of a listener for keypress or something, count the characters and eat up anything beyond 4.

Rob LeVineRob LeVine replied 2 years ago

Try this https://paste2.org/pfN6DHYe , making sure to change the field id to match yours.

Danier Valiev replied 2 years ago

Hi Rob, thank you for your reply and for the code. And sorry for late response. Where do you usually type/write the code in formidable pro? Whenever i type code inside the "Before Field" or in "After Field" or anywhere in Customize html, it just doesnt want to react. How and where do you write custom js code for stuff to work with formidable?

Rob LeVineRob LeVine replied 2 years ago

Post a screenshot of exactly what you have in your After Fields (or Before Fields) section. Those are the locations in which to place scripts.

Danier Valiev answered 2 years ago
I found out i can use the Phone field to achieve what I want. It should of be the same for number field too. Attached image
Attachments
Rob LeVineRob LeVine replied 2 years ago

See above comment. A phone field is "tel" type, which isn't "number" so it works in that case.

Danier Valiev answered 2 years ago

Hi @Rob LeVine,

Here is a screenshot of what I wrote in the Before field. 
I tried with both Field ID and Field Key. none of them react.
Also when I want to add other scripts like interest rate calculations it just doesnt want to react. 

Attachments
Rob LeVineRob LeVine replied 2 years ago

There is no chance your field id is 3hyc72_myrestrictednumberfield. You want the id of the field and it's definitely going to start with field_ and then append the field key as seen in your field options screen. So I'm guessing that it should be #field_3hyc72. You can also tell what the field id is by using the browser developer tools and inspecting the element. If correcting the id doesn't work, use the developer tools to show the debug console and make sure there are no jquery errors. Also, I prefer putting the jQuery code in the After Fields section.

Danier Valiev answered 2 years ago
@Rob LeVine 

Is it not the "Field Key" you use or is it the ID number that is being used? 
Attachments
Rob LeVineRob LeVine replied 2 years ago

It is field_ plus the field key. I suggest you use the developer tools (F12) to inspect the object as that will help you identify it.

Rob LeVineRob LeVine replied 2 years ago

"field_" plus the field key is the field's ID. The formidable ID is not relevant in this case.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right