Change requirement level at runtime
I've made quite some posts around how to dynamically modify the form by using JavaScript. One thing I haven't posted around yet, but what I do use quite often, is to modify the requirement level of attributes on the form at runtime. Based on a selection in the system, another field could become required. Also if the selection changes again, then the field should not be required anymore. Here is the code for making a field optional, recommended or required:
// Set field to not required
crmForm.SetFieldReqLevel("fieldname", 0);
// Set field to business required
crmForm.SetFieldReqLevel("fieldname", 1);
// Set field to business recommended
crmForm.SetFieldReqLevel("fieldname", 2);
Update: new code placed based on the comment by Peter. Before the update I had this piece of code:
// set the field required (i.e. show error message when field is not filled in)
crmForm.all.fieldname.req = 2;
// modify the label to be red
crmForm.all.fieldname_c.className = 'req';