One field cannot be greater than another field

  • msjayum
    Asked on March 24, 2024 at 8:43 AM

    Hi,

    Can I get the CSS code for not allowing one field to be greater than another field? (both field are number valued)

    #input_108 must be less than or equal to #input_152

    Thank you

  • Jovanne JotForm Support
    Replied on March 24, 2024 at 9:07 AM

    Hi Kimberly,

    Thanks for reaching out to Jotform Support. Unfortunately, what you're trying to do is not possible with CSS codes. Keep in mind that CSS is only used for the styling of your form and not for the calculation and logic. What we can do is use a Show/Hide field condition that will show a warning message if the first number is greater than the second number. Here's how to do it:

    1. Add a Paragraph field that will show a warning message if field 1 is greater than field 2.

      One field cannot be greater than another field Image 1 Screenshot 30
    2. Add the Show/Hide field condition to show the Warning Text and Hide the Submit button.

      One field cannot be greater than another field Image 2 Screenshot 41

    You can check and clone my demo form here.

    Give it a try and let us know if you need any help.

  • msjayum
    Replied on March 27, 2024 at 3:11 AM

    Hi,

    Thank you for the idea.

    I've done it and it works nicely.


  • FSCAA
    Replied on April 18, 2024 at 7:54 AM

    I do not have a Greater Than option on the Show/Hide Field formula

    One field cannot be greater than another field Image 1 Screenshot 20 Screenshot 10

  • Jovanne JotForm Support
    Replied on April 18, 2024 at 7:57 AM

    Hi FSCAA,

    As for your question, I've moved that to a new thread. You can check that out here.

    Let us know if there’s anything else we can do for you.

  • agtinstitute488
    Replied on April 18, 2024 at 8:09 AM

    You can achieve this with CSS by using the :invalid and :valid pseudo-classes along with the min and max attributes in HTML. However, CSS alone cannot directly compare two input fields and enforce such validation. You'll need to use JavaScript for that. Here's how you can set up the CSS to style the inputs based on their validity:

    html

    Copy code
    <style> /* Default styling for the inputs */ input[type="number"] { width: 100px; /* Adjust width as needed */ } /* Style inputs when they are valid */ input[type="number"]:valid { border: 1px solid green; /* Green border for valid inputs */ } /* Style inputs when they are invalid */ input[type="number"]:invalid { border: 1px solid red; /* Red border for invalid inputs */ } </style>

    However, to enforce the condition where #input_108 must be less than or equal to #input_152, you'll need JavaScript. Here's a basic example using jQuery:

    html

    Copy code
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { $('#input_108, #input_152').on('input', function() { var input108 = parseFloat($('#input_108').val()); var input152 = parseFloat($('#input_152').val()); if (input108 > input152) { $('#input_108').val(''); // Clear input if invalid alert('Input 108 must be less than or equal to Input 152'); } }); }); </script>

    Make sure to replace #input_108 and #input_152 with the actual IDs of your input fields. This script will check the values of both input fields whenever they are changed, and if #input_108 is greater than #input_152, it will clear the value of #input_108 and display an alert message.

    FOR MORE VISIT US

  • FSCAA
    Replied on April 18, 2024 at 10:52 AM

    Wow. Wouldn't it be easier to add a Maximum Age to the properties options in addition to the Minimum Age?