I have a page with 3 tables:
<table id="t1">
<tr>
<td></td><td><input type="text" name="name" /></td>
<td></td><td><input type="text" name="id" /></td>
<td></td><td><input type="text" name="perDate" /></td>
<td></td><td><input type="text" name="email" /></td>
</tr>
</table>
<table id="t2">
<tr>
<td></td><td><input type="text" name="lostFound" /></td>
<td></td><td><input type="text" name="lostDate" /></td>
<td></td><td><input type="text" name="item" /></td>
</tr>
</table>
<table id="t3">
<tr>
<td></td><td><input type="text" name="check" /></td>
<td></td><td><input type="text" name="reason" /></td>
<td></td><td><input type="text" name="chkDate" /></td>
</tr>
</table>
So, I have a validation script where if I just change the class to ‘required’ then it will make that input field required. The problem I am having is….I only want to require a field if another field in that table is filled out, otherwise the field is not required. Ex: If t1 – name is filled in then I want to change the class of t1 – id, t1 – perDate, and t1 – email to ‘required’, and leave all of the other classes for t2 and t3 untouched. I was wondering if someone could point me in the right direction with some sort of javascript code that would make this possible. thanks for all the help, and if more info is needed, please ask.
So when you blur out of the “Name” input, you require the adjacent inputs to be required if they provided a name. But also don’t forget to make them not required if they go back and decide to clear that name field like so:
Also, if you’re trying to change the required attribute if any of the controls have data, you would do something like this: