I have been struggling to get a conditionally required field to show a red asterisk via javascript for a few days now and finally decided I need some help figuring out what I'm doing wrong.
I have a form with two fields, one field would be a required field IF yes is selected in the first field, below is the current configuration and additional rules I have currently set up for these two fields:
FieldName: Eagle Type: Radial Group Options: No (ID:2) Yes (ID:1)
Field Name: EagleDetails Type: Text Box
Rules: 1) If yes is selected for Eagle field, EagleDetails field will show, otherwise EagleDetails field is hidden. 2) If yes is selected for Eagle AND EagleDetails is left empty, error message is shown and form cannot be submitted.
I would like to have a red asterisk shown next to EagleDetails field when yes is selected for Eagle. I have attempted to utilize the cfAddFieldRequired(formId, fieldName) [written as: cfAddFieldRequired(formId, EagleDetails); in JavaScript to run, with Eagle entered into Field Name for rule definition and Eagle:1 entered into Condition]. I have also tried writing out the full custom JavaScript utilizing the former makeRequired documentation from this link. Using that, I entered Eagle under Field Name in the rule definition and makeRequired(formName, formId); in the JavaScript to run field in rule definition and Eagle:1 in condition. I've included the java code snippet as I had edited it for my situation.
<script>
function makeRequired(formName, formId){
var myFieldLabel = AJS.$('#' + formId).find('#i_labelfor_EagleDetails');
AJS.$(myFieldLabel).find('span').remove();
AJS.$(myFieldLabel).append('<span style="color:red;font-weight: bolder;float: right;font-size: larger;">* </span>');
}
function removeRequired(formName, formId){
var myFieldLabel = AJS.$('#' + formId).find('#i_labelfor_EagleDetails');
AJS.$(myFieldLabel).find('span').remove();
}
</script>
May be you can use the technique and the JS function that is bundled with ConfiForms to do that - https://wiki.vertuna.com/display/CONFIFORMS/JavaScript+functions
Might be easier
Literally just a field definition rule with just one liner (demo adds it to the field named "somefield")
cfAddFieldRequired(formId, 'somefield')
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.