cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <!DOCTYPE html>
<html lang="en">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <title>Accessible Form Example</title>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
   </head>
   <body>
     <div id="content-wrapper">
      <h1>Accessible form validation example:</h1>
      <p>The following is an example of a keyboard accessible form. Notice the following accessibility features: visible focus indicator, accessible tooltips, labels for every field, and accessible error messaging. </p>
        <p>Notice how the "First Name" field has a hidden message within the field's label for the screen reader, then a visible message for the sighted user in a slightly different spot. This is what you need to do if the design shows the error message in a place that precludes you from placing it within the field's label. </p>
      <p>Try it out yourself by tabbing through this page with your keyboard and screen reader. Purposely leave fields blank and see what happens. Let me know if you notice any issues.</p>
      <hr>
      </hr>
      <h2>Sample Form</h2>
      <form action="#" name="myForm" onsubmit="return validateForm()" method="get">
         <p>Required fields are indicated with a "<span class="reqField">*</span>".</p>
         <p><label for="fname">First Name<span class="reqField">*</span>:<span class="element-invisible" id="fnameErrorHidden"></span></label><br />
            <input id="fname" type="text" name="textfield"/>
            <span class="errorMsg" id="fnameError"></span>
         </p>
         <div><label for="sample">Sample field with tooltip<span class="reqField">*</span>:&nbsp;<span class="errorMsg" id="sampleError"></span></label><br />
            <input id="sample" type="text" name="textfield2" aria-describedby="sample-tip"/>
         <div role="tooltip" id="sample-tip">Hint: Type anything here.</div>
         </div>
         <p>
            <fieldset>
               <legend tabindex="0" id="toppingsLegend">Select your pizza toppings<span class="reqField">*</span>:<span class="errorMsg" id="toppingsLegendError"></span></legend>
               <input id="ham" type="checkbox" name="toppings" value="ham"/>
               <label for="ham">Ham</label><br>
               <input id="pepperoni" type="checkbox" name="toppings" value="pepperoni">
               <label for="pepperoni">Pepperoni</label><br>
               <input id="mushrooms" type="checkbox" name="toppings" value="mushrooms">
               <label for="mushrooms">Mushrooms</label><br>
               <input id="olives" type="checkbox" name="toppings" value="olives">
               <label for="olives">Olives</label>
            </fieldset>
         </p>
         <fieldset>
            <legend tabindex="0" id="shippingLegend">Choose a shipping method<span class="reqField">*</span>:<span class="errorMsg" id="shippingLegendError"></span></legend>
            <input id="overnight" type="radio" name="shipping" value="overnight"/>
            <label for="overnight">Overnight</label><br>
            <input id="twoday" type="radio" name="shipping" value="twoday">
            <label for="twoday">Two day</label><br>
            <input id="ground" type="radio" name="shipping" value="ground">
            <label for="ground">Ground</label>
         </fieldset>
         <label id="favcityLabel" for="favcity" tabindex="0">Choose your favorite city?<span class="reqField">*</span><span class="errorMsg" id="favcityError"></span></label><br />
         <select id="favcity" name="select" aria-labelledby="favcityLabel">
            <option value="0">Select</option>
            <option value="1">Amsterdam</option>
            <option value="2">Buenos Aires</option>
            <option value="3">Delhi</option>
            <option value="4">Hong Kong</option>
            <option value="5">London</option>
            <option value="6">Los Angeles</option>
            <option value="7">Moscow</option>
            <option value="8">Mumbai</option>
            <option value="9">New York</option>
            <option value="10">Sao Paulo</option>
            <option value="11">Tokyo</option>
         </select>
         <div><input type="submit" name="submit" value="Submit"/>
            <input type="reset" name="reset" value="Reset"/>
         </div>
     </form></div>
   </body>
</html>
            
          
!
            
                            /* Accessible form styles begin */
label, fieldset {
	font-weight: bold;
}
fieldset label, input {
	font-weight: normal;
}
fieldset {
	margin-bottom: 20px;
  padding: 5px;
}
select {
	margin-bottom: 30px;
  font-size:14px;
}
.errorMsg, .reqField {
	color:#bb4e00;
}
.errorMsg {
	font-weight: normal;
}
input {
  font-size:16px;
  padding: 4px;
  color:#333;
}
/* Accessible form styles end */

/* hides content from sighted users, can be accessed by screen reader */
.element-invisible {
	position: absolute !important;
	height: 1px; width: 1px; 
	overflow: hidden;
	clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
	clip: rect(1px, 1px, 1px, 1px);
}

/* set up hidden tooltip */
[role="tooltip"] {
	display: none;
	padding: 0.25em;
	margin: 0;
	color: #666;
	width: 100%;
	z-index: 2;
	font-weight: normal;
	font-style: italic;
}

/* reveal associated tooltip on focus of preceeding input */
input:focus + [role="tooltip"] {
	display: block;
}

/* Reference: http://tympanus.net/codrops/2012/10/04/custom-drop-down-list-styling/ */

/* GLOBALS */
*,
*:after,
*:before {
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;
	padding: 0;
  font-family: sans-serif;
	/*    margin: 0; */
}

::selection {
	background: white; 
}

::-moz-selection {
	background: red; 
}

#content-wrapper {
  width: 60%;
  margin: 0 auto;
  min-width: 320px;
  max-width: 800px;
}


            
          
!
            
              //Form validation
var formValid;

function validateForm() {
	//set initial value of formValid to true
	formValid = 1;
	resetErrors();


	//validate select menu
	var z = document.forms["myForm"]["favcity"].value;
	if (z == 0) {
		applyError("favcity","&nbsp;You must choose a favorite city.");
	}
	//validate radio buttons
	if(validateGroup("shipping")==false) {
		document.getElementById("shippingLegendError").style.padding = "0 5px";
		applyError("shippingLegend","Please pick a shipping method.");
	}
	//validate checkboxes
	if(validateGroup('toppings')==false) {
		document.getElementById("toppingsLegendError").style.padding = "0 5px";
		applyError("toppingsLegend","You must pick at least one topping.");
	}
	//validate input fields
	var x = document.forms["myForm"]["sample"].value;
	if (x == "") {
		applyError("sample","&nbsp;You must enter some text here.");
	}
	var y = document.forms["myForm"]["fname"].value;
	if (y == "") {
		document.getElementById("fnameErrorHidden").innerHTML = "You must enter your first name.";
		applyError("fname","&nbsp;You must enter your first name.");
	}
	//if a form field is not valid, do not submit form
	if (formValid == 0){
		return false;
	}
}

//Validates Instruments custom select menu


//Appends error message to label, puts focus on field with error message
function applyError(errorFieldId,errorMessage){
	var errorMessageId = errorFieldId + "Error";
	document.getElementById(errorMessageId).innerHTML = errorMessage; //puts error in span tag
	document.getElementById(errorFieldId).focus(); //puts focus on field with error
	formValid = 0; //sets global formValid variable to false
}

//validates grouped form fields like checkboxes or radio buttons. Accepts the name of the group to be validated.
function validateGroup(groupName){
	var group = document.getElementsByName(groupName);
	var groupCount = 0;
	for (var i = 0; i < group.length; i++) {
		if (group[i].checked) {
			groupCount++;
		}
	}
	if (groupCount < 1) {
		return false;
	}
	return true;
}

//resets error messages so they are turned off
function resetErrors(){
	document.getElementById("fnameError").innerHTML = "";
	document.getElementById("fnameErrorHidden").innerHTML = "";
	document.getElementById("sampleError").innerHTML = "";
	document.getElementById("toppingsLegendError").innerHTML = "";
	document.getElementById("shippingLegendError").innerHTML = "";
	document.getElementById("favcityError").innerHTML = "";
}
            
          
!
999px
Loading ..................

Console