Any fields that are required will use the aria-required
attribute which you'll be able to access and will show as aria-invalid
if there's an error. You have a couple of options for handling your situation. One option would be to utilize the capabilities of a jQuery plug-in like the jQuery Validation Plug-in to check for fields which aren't visible or that have null values, etc, then display them before allowing a save. Any required fields which are hidden (aria-hidden
), can then be made visible for editing before the user is allowed to save.
Since you're already manipulating visibility, you can also simply check for the error flag that would appear using your own code and then display it the field along with an error message. Below is an example using jQuery of how to validate a form that you could modify for your purposes without the need of a plug-in. I believe the jQuery Validation Plug-in essentially automates the process of looking for or manipulating the aria
attributes while this type of code would allow you to access them directly.
<!DOCTYPE HTML>
<html lang="en">
<head> <title>Input Error Notification with aria-live=assertive and aria-invalid using jQuery</title>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(e) {
$('#signup').submit(function() {
$('#errText').html('');
$('input').removeAttr("aria-invalid");
$('label').removeAttr("class");
var eFlag = 0;
if ($('#first').val() === '') {
$('#first').attr("aria-invalid", "true");
$("label[for='first']").addClass('failed');
eFlag++;
}
if ($('#last').val() === '') {
$('#last').attr("aria-invalid", "true");
$("label[for='last']").addClass('failed');
eFlag++;
}
if ($('#email').val() === '') {
$('#email').attr("aria-invalid", "true");
$("label[for='email']").addClass('failed');
eFlag++;
}
if (eFlag > 0) {
$('#errText').html("Please complete all required fields (" + eFlag + ") and retry").focus();
}
return false;
});
});
</script>
<style type="text/css">
label.failed {
border: red thin solid;
}
</style>
</head>
<body>
<h2 tabindex="-1" id="errText" aria-live="assertive"></h2>
<form name="signup" id="signup" method="post" action="#">
<p>
<label for="first">First Name (required)</label><br>
<input type="text" name="first" id="first">
</p>
<p>
<label for="last">Last Name (required)</label><br>
<input type="text" name="last" id="last">
</p>
<p>
<label for="email">Email (required)</label><br>
<input type="text" name="email" id="email">
</p>
<p>
<input type="submit" name="button" id="button" value="Submit">
</p>
</form>
<hr />
<p style="margin: 0;" role="contentinfo">Created by Sailesh Panchang, Deque Systems | December 27, 2013</p>
</body>
</html>
<input>
tag?