In some cases, you might need to validate a form field on the front end, ensuring a user doesn’t input invalid data.

In this tutorial, without relying on server-side validation we will be preventing a negative numbers in Textbox Fields.

Instead of implementing a full-fledged validation plugin, you can use a simple JavaScript code snippet. This is where the JavaScript section in Formea Form Builder comes in handy.

Example

Try entering a value less than zero in the input field below, the field should reset to empty and an error message prompt.

Ex: 5

Where to find the element Alias and ID

In order to manipulate the field value, we need to know the element alias and ID.

Navigate to
Formea Form Builder > Elements > [THE_TARGETED_ELEMENT].
See screenshot to locate the element alias and ID.

Locate the element alias and ID

Steps to Implement JavaScript for Field Manipulation

  1. Navigate to Your Form in Formea:

    Go to Joomla! Administrator > Components > Formea Form Builder > Forms > [YOUR_FORM_NAME].

  2. Access the JavaScript Section:

    • Click on the "CSS/JavaScript" tab.
    • Scroll to the "JavaScript Declaration" section where you'll add your custom JavaScript.
    Formea Javasacript Declaration
  3. Add the JavaScript Code: Below is a sample JavaScript code that prevents negative numbers from being entered in a specified field. Replace "MY_ELEMENT_ALIAS" with the field's alias and "MY_ELEMENT_ID" with the field's ID.

  4. Save your form:
    Click "Save" or "Save & Close" to store your changes.

JavaScript Code

This is an example of code designed to detect an input value and determine whether it’s a positive or negative number.

For better readability and flexibility—allowing for reuse or modification to accommodate other elements or fields—we will extract the logic into functions.

setUpValidation:

This function sets the input’s minimum value and attaches an event listener to monitor changes. It calls updateValidationState to update the input's appearance based on its validity.

updateValidationState:

This function handles toggling classes for valid/invalid feedback and updates the inner text of the error message div. The classes are based on a template that uses the Bootstrap CSS framework, like Joomla’s default Cassiopeia template. You may need to adjust the classes to match your own template.

Feel free to modify this according to your requirements.

/* JS to detect element input value changes */ function listenToChanges() { const myElementAlias = "MY_ELEMENT_ALIAS"; /* Change this to your element alias */ const myElementId = "MY_ELEMENT_ID"; /* Change this to your element ID */ const elementId = `formea_${myElementAlias}_${myElementId}_[FORMEA_ID]_[FORMEA_TOKEN]`; const myElement = document.getElementById(elementId); if (myElement) { const errorMessageDiv = myElement.nextElementSibling; setUpValidation(myElement, errorMessageDiv); } else { console.error(`Element with ID "${elementId}" not found.`); } } function setUpValidation(element, errorMessageDiv) { // Set minimum value to 0 element.setAttribute('min', 0); // Add event listener to prevent negative input element.addEventListener('input', function () { const value = parseFloat(this.value); if (value < 0) { // Reset value and show error if a negative number is entered this.value = ''; updateValidationState(element, errorMessageDiv, false, 'Invalid! Value must be more than -1'); } else { // Clear the error message for valid values updateValidationState(element, errorMessageDiv, true); } }); } function updateValidationState(element, errorMessageDiv, isValid, message = '') { if (isValid) { element.classList.remove('is-invalid'); if (errorMessageDiv) { errorMessageDiv.classList.remove('invalid-feedback'); errorMessageDiv.classList.add('valid-feedback'); errorMessageDiv.innerHTML = message; } } else { element.classList.add('is-invalid'); if (errorMessageDiv) { errorMessageDiv.classList.remove('valid-feedback'); errorMessageDiv.classList.add('invalid-feedback'); errorMessageDiv.innerHTML = message; } } } // Run the function after the DOM has loaded document.addEventListener("DOMContentLoaded", listenToChanges); /* END JS */
On this page