Kentico 11, text field value acceptance dependent on drop down

Tom Wisneski asked on March 27, 2020 15:18

Question: Setup is Kentico 11, backend is Webforms. We have a form with a dropdown for Feet and Metric altitudes. Then there is a text field the user inputs an altitude. So if the user selects Metric in the dropdown, then inputs a value that is higher than we accept (in metric measurement). Then an error message should display. Likewise the same with 'Feet' and the value in the text field input. So with using dependent fields, I am not sure how a macro could be written for this. Pretty new at the macro stuff. Any help is appreciated. We have to do the same with a temperature scale option also. (Celsius & Fahrenheit). Thanks. Tom W.

Correct Answer

Dmitry Bastron answered on March 30, 2020 14:24

Hi Tom,

You can do it the following way. Assume:

  • A text field called Degrees with a dropdown of "celsius" and "fahrenheit" (and you don't need to check either "Has depending fields" or "Depends on another field" checkboxes by the way)
  • An integer field called DegreesValue with the following Validation rule base on General condition to allow censius values from 20 to 40 or fahrenheit from 0 to 100:

    (Fields["Degrees"].Value == "celsius" && Value > 20 && Value < 40) || (Fields["Degrees"].Value == "fahrenheit" && Value > 0 && Value < 100)

To give you an idea how it can be improved further more, have a look at Macro rules > Form validation > Compare to another field. Based on this rule I think you would be able to combine your custom rule and reuse it easily from form to form if needed.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on March 28, 2020 01:46

I'd suggest having 2 input fields, one for feet and one for metric. Then do the following:

For the feet field, make sure the field is NOT required.
For the feet field, check the "has depending fields" box.
For the feet text input, check the "depends on another field" box.
For the feet text input, set the enabled property equal to a macro of FeetMacroDropDownFieldName.Value == 'feet'.
For the feet text input, set the validation rule = maximum value and set your max value.
Duplicate the same for the metric field.

Doing it this way simplifies things a bit so you don't have to try to do it all in one field.

0 votesVote for this answer Mark as a Correct answer

Tom Wisneski answered on March 29, 2020 19:23

Brenden, I do appreciate your response...and I always look to your posts as exceptionally helpful in resolving Kentico issues / work-arounds. However with this reply, I will have to look to another work-around. For now I have done the validation with jQuery, being the design will have as follows; a dropdown for "Feet or Metric", with a user input field. Likewise, a dropdown for "Celsius and Fahrenheit" with a user input field. This has been decided as for the user input. So offered post would not be a usable solution. Thanks. :)

0 votesVote for this answer Mark as a Correct answer

Tom Wisneski answered on March 30, 2020 15:29

Dmitry. Thank you for the reply. This sounds very promising. I will try this option...look at the material on Macro rules > Form validation > compare to another field, documentation. Thanks!

0 votesVote for this answer Mark as a Correct answer

Tom Wisneski answered on March 30, 2020 15:53

Dmitry...thanks, it now seems so simple after struggling with this for a couple of hours. Thank you!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.