Digital pattern library

Form elements

Keep forms as simple as possible.

Options


Fieldsets and legends

Use fieldsets to group related form controls. The first element inside a fieldset must be a legend element which describes the group.

Example
Personal details
Enter your postcode without any spaces. For example, 'KY169AL'.
Code
<fieldset><legend>Personal details</legend>
	<div class="form-group">
		<label class="control-label" for="firstName">First name</label>
		<input class="form-control" id="firstName" type="text">
	</div>
	<div class="form-group">
		<label class="control-label" for="lastName">Last name</label>
		<input class="form-control" id="lastName" type="text">
	</div>	<div class="form-group">
		<label class="control-label" for="Postcode">Postcode</label>
			<span class="help-block">
				Enter your postcode without any spaces. For example, 'KY169AL'.
		  	</span>
		<input class="form-control" id="postcode" type="text">
	</div></fieldset>

Labels

Example
Code
<div class="form-group">
	<label class="control-label" for="name">Name</label>
	<input class="form-control" id="name" type="text">
</div>

Hint text

Example
Enter your postcode without any spaces. For example, 'KY169AL'.
Code
<div class="form-group">
	<label class="control-label" for="Postcode">Postcode</label>
		<span class="help-block">
			Enter your postcode without any spaces. For example, 'KY169AL'.
	  	</span>
	<input class="form-control" id="postcode" type="text">
</div>

Input fields

Default input

Example
Code
<div class="form-group">
	<label class="control-label" for="firstName">First name</label>
	<input class="form-control" id="firstName" type="text">
</div>
<div class="form-group">
	<label class="control-label" for="lastName">Last name</label>
	<input class="form-control" id="lastName" type="text">
</div>

Input addons

Addons can be used when the input field is always going to be prefixed with a particular value.

Example
£
.00
Code
<form action="" class="form">
	<div class="form-group">
		<label for="input-addon">Cost</label>
		<div class="input-group">
			<div class="input-group-addon">£</div>
			<input type="text" class="form-control" id="input-addon">
			<div class="input-group-addon">.00</div>
		</div>
	</div>
</form>

Textarea

Allows for multiple lines of text.

Example
Code
<div class="form-group">
    <label for="textArea" class="control-label">Textarea</label>
	<textarea class="form-control" rows="3" id="textArea"></textarea>
</div>

File upload

Uses the native input using type="file". This is so:

Example
Code
<div class="form-group">
	<label class="form-label" for="file-input">Upload a file</label>
	<input type="file" id="file-input">
</div>

Checkboxes

Inline checkboxes

Example

Please choose one or more options:

Code
<div class="form-group">
    <div class="checkbox-box inline">
    <label>Options</label>
    <p>Please choose one or more options:</p>
        <ul>
            <li>
                <input type="checkbox" value="option1" id="checkbox-inline1">
                <label for="checkbox-inline1">One</label>

                <div class="check glyphicon glyphicon-ok"></div>
            </li>

            <li>
                <input type="checkbox" value="option2" id="checkbox-inline2">
                <label for="checkbox-inline2">Two</label>

                <div class="check glyphicon glyphicon-ok"></div>
            </li>

            <li>
                <input type="checkbox" value="option3" id="checkbox-inline3">
                <label for="checkbox-inline3">Three</label>

                <div class="check glyphicon glyphicon-ok"></div>
            </li>
        </ul>
    </div>
</div>

Stacked checkboxes

Example

Please choose one or more options:

Code
<div class="form-group">
    <div class="checkbox-box">
    <label>Options</label>
    <p>Please choose one or more options:</p>
        <ul>
            <li>
                <input type="checkbox" value="option1" id="checkbox-stacked1">
                <label for="checkbox-stacked1">One</label>

                <div class="check glyphicon glyphicon-ok"></div>
            </li>

            <li>
                <input type="checkbox" value="option2" id="checkbox-stacked2">
                <label for="checkbox-stacked2">Two</label>

                <div class="check glyphicon glyphicon-ok"></div>
            </li>

            <li>
                <input type="checkbox" value="option3" id="checkbox-stacked3">
                <label for="checkbox-stacked3">Three</label>

                <div class="check glyphicon glyphicon-ok"></div>
            </li>
        </ul>
    </div>
</div>

Radio buttons

Inline radio buttons

Example

How many fudge donuts would you like?

Code
<div class="form-group">
    <div class="radio-button inline">
    <label>Options</label>
    <p>How many fudge donuts would you like?</p>
        <ul>
            <li>
                <input type="radio" id="inline-option1" name="inline-selector">
                <label for="inline-option1">One</label>

                <div class="check"></div>
            </li>

            <li>
                <input type="radio" id="inline-option2" name="inline-selector">
                <label for="inline-option2">Two</label>

                <div class="check"></div>
            </li>

            <li>
                <input type="radio" id="inline-option3" name="inline-selector">
                <label for="inline-option3">Three</label>

                <div class="check"></div>
            </li>
        </ul>
    </div>
</div>

Stacked radio buttons

Example

How many fudge donuts would you like?

Code
<div class="form-group">
    <div class="radio-button">
    <label>Options</label>
    <p>How many fudge donuts would you like?</p>
        <ul>
            <li>
                <input type="radio" id="stacked-option1" name="stacked-selector">
                <label for="stacked-option1">One</label>

                <div class="check"></div>
            </li>

            <li>
                <input type="radio" id="stacked-option2" name="stacked-selector">
                <label for="stacked-option2">Two</label>

                <div class="check"></div>
            </li>

            <li>
                <input type="radio" id="stacked-option3" name="stacked-selector">
                <label for="stacked-option3">Three</label>

                <div class="check"></div>
            </li>
        </ul>
    </div>
</div>

Conditionally revealing content

Wrap the input you wish to hide with the following code:

<div class="conditional-content" aria-hidden="true">
	[Your hidden input]
</div>

Then use the following JavaScript code to show/hide the conditional content:

$(document).ready(function() {
    $('input.input-name').change(
        function() {
            var val = $(this).val();

            if(val === "option-value") {
                $(".conditional-content").show();
            }
            else if(val !== "option-value") {
                $(".conditional-content").hide();
            }
        }
    );
});
Example

How many fudge donuts would you like?

Code
<div class="form-group">
	<div class="radio-button">
    <label>Options</label>
    <p>How many fudge donuts would you like?</p>
	    <ul>
	        <li>
	            <input type="radio" id="stacked-1" class="conditional-radio" name="conditional" value="one">
	            <label for="stacked-1">One</label>

	            <div class="check"></div>
	        </li>
	        <li>
	            <input type="radio" id="stacked-2" class="conditional-radio" name="conditional" value="two">
	            <label for="stacked-2">Two</label>

	            <div class="check"></div>
	        </li>
	        <li>
	            <input type="radio" id="stacked-3" class="conditional-radio" name="conditional" value="three">
	            <label for="stacked-3">Three</label>

	            <div class="check"></div>
	        </li>
	        <li>
	            <input type="radio" id="stacked-4" class="conditional-radio" name="conditional" value="other">
	            <label for="stacked-4">Other</label>

	            <div class="check"></div>
	        </li>
	    </ul>
	</div>
	<div class="conditional-content" aria-hidden="true">
		<label class="control-label" for="name">Other</label>
		<input class="form-control" id="name" type="text">
	</div>
</div>

Select boxes

If possible, avoid using select boxes (drop-down lists). Use radio buttons or checkboxes instead.

Example
Code
<div class="form-group">
	<label class="form-label" for="select-box">Status</label>
	<select class="form-control" id="select-box">
		<option value="">Please select one of the following...</option>
		<option value="ug">Undergraduate</option>
		<option value="pgr">Research postgraduate</option>
		<option value="pgt">Taught postgraduate</option>
	</select>
</div>