custom stylable validation bubbles with with custom errormessages
Removes the native error tooltip of HTML5 capable browsers and replaces it with a styleable validation hint.
by trixta
HTML
<script src="http://afarkas.github.com/webshim/demos/js-webshim/minified/extras/modernizr-custom.js"></script>
<script src="http://afarkas.github.com/webshim/demos/js-webshim/minified/polyfiller.js"></script>
<form class="example">
<div>
<label for="your-name">Name<sup>1</sup>: *</label>
<input type="text" required="required" id="your-name" name="yourName" />
</div>
<div>
<label for="email">email<sup>1</sup>: *</label>
<input type="email" required="required" id="email" name="email" />
</div>
<div>
<label for="name-custom">custom error-message<sup>2</sup>: *</label>
<input required="required" type="text" id="name-custom" name="name-custom" data-errormessage="Please enter something" title="" />
</div>
<div>
<label for="multiple-name-custom">custom error-message <sup>3</sup>: *</label>
<input required="required" type="email" id="multiple-name-custom" name="multiple-name-custom" data-errormessage='{"valueMissing": "Please enter an email address", "typeMismatch": "This is not a valid email address. Please enter a valid one"}' title="" />
</div>
<div>
<input type="submit" />
</div>
</form>
<ol>
<li>1. uses native or polyfilled messages for error</li>
<li>2. uses custom error message with data-errormessage attribute</li>
<li>3. implements different messages for valueMissing and typeMismatch (email) using data-errormessage attribute with JSON</li>
<p>Note: Although the <a href="http://afarkas.github.com/webshim/demos/demos/webforms.html">forms feature</a> of <a href="http://afarkas.github.com/webshim/demos/index.html" target="_blank">webshims lib</a> implements the hole HTML form constraint validation API, the example is based on extensions to make the API easier to use.</p>
CSS
body {
font: 100% arial,helvetica,sans-serif;
}
.validity-alert span.va-box {
box-shadow: 2px 2px 5px #E8673B;
background: rgb(255, 255, 255);
background: rgba(255, 255, 255, 0.9);
color: #000;
font: 12px arial;
}
.example {
margin: 10px;
padding: 1px 8px;
background: #fff6c6;
border-radius: 10px;
width: 420px;
}
.example > div:after {
content: " ";
display: block;
clear: both;
}
/* animate all */
.example > div,
.example input[type=text],
.example input[type=email] {
-moz-transition-property: all;
-moz-transition-duration: 1s;
-webkit-transition-property: all;
-webkit-transition-duration: 1s;
-o-transition-property: all;
-o-transition-duration: 1s;
-ms-transition-property: all;
-ms-transition-duration: 1s;
transition-property: all;
transition-duration: 1s;
}
.example > div {
margin: 15px 10px;
padding: 5px 10px;
border-radius: 10px;
background: #ffe268;
box-shadow: 3px 3px 3px #e0c75b;
}
.example > div.invalid {
background: #e8673b;
box-shadow: 3px 3px 3px #e8673b;
}
.example > div.valid {
background: #b0ea74;
box-shadow: 3px 3px 3px #a3d96c;
}
.example input {
box-shadow: none;
}
.example input[type=text],
.example input[type=email] {
float: right;
display: inline-bock;
padding: 0.3em;
height: 1.1em;
width: 180px;
border: 1px solid #d8bf58;
border-radius: 4px;
box-shadow: inset 1px 1px 1px #e0c75b;
background: #fff;
}
.example > div.valid input {
border: 1px solid #9bce67;
box-shadow: inset 1px 1px 1px #87b45a;
}
.example > div.invalid input {
border: 1px solid #a81107;
box-shadow: inset 1px 1px 1px #890e06;
}
.example label {
margin: 0.1em 0 0;
cursor: pointer;
display: inline-block;
vertical-align: middle;
}
.example input[type=submit] {
width: 100%;
text-align: center;
}
JavaScript
(function() {
"use strict";
//load HTML5 form features, if not implemented + some extensions
$.webshims.polyfill('forms');
$(function() {
$('form').bind('changedvalid', function(e) {
$(e.target).parent().addClass('valid').removeClass('invalid');
}).bind('changedinvalid', function(e) {
$(e.target).parent().addClass('invalid').removeClass('valid');
}).bind('firstinvalid', function(e) {
$.webshims.validityAlert.showFor(e.target);
return false;
});
});
})();