Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Conditionally require a field
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Toggle field requirement</title>
<meta name="description" content="A clean-room implmentation of conditional field requirement.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="//"></script>
$(function() {
$('#no-doi, #mint-doi').change(function(e){
var $target = $(,
$field = $('#publisher')
if ($target.attr('id') == 'mint-doi') {
$field.attr('required', 'true');
} else {
fieldset {margin:1em 0}
form {max-width:30em}
label {display:block}
<form action="/">
<legend>Create Work</legend>
<label for="title">Title</label>
<input id="title" name="title" required="true"/>
<label for="publisher">Publisher</label>
<input id="publisher" name="publisher"/>
<label for="no-doi">
<input type="radio" name="doi" id="no-doi" checked="checked"/>
I don't need a DOI
<input type="radio" name="doi" id="mint-doi"/>
Create a DOI for me
<input type="submit" value="Submit"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment