Skip to content

Instantly share code, notes, and snippets.

@jamesflorentino
Created September 18, 2012 18:48
Show Gist options
  • Save jamesflorentino/3745010 to your computer and use it in GitHub Desktop.
Save jamesflorentino/3745010 to your computer and use it in GitHub Desktop.
HTML5 form validator example
<!doctype>
<html>
<head>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<form>
<h4>User name</h4>
<input
type = "text"
name = "name"
placeholder = "Enter username"
pattern = "[a-zA-Z0-9.]{3,16}"
maxlength = "16"
required
/>
<i class="status"></i>
<h4>E-mail</h4>
<input
type="email"
name="email"
placeholder="Your E-mail"
required
/>
<i class="status"></i>
<h4>Website <i>Optional</i></h4>
<input
type="url"
name="website"
placeholder="Your Website URL"
/>
<button>Submit</button>
</form>
</body>
</html>
/** Initial styling **/
/** ========================== **/
body {
background : #eee;
font-family : Helvetica Neue, Arial, sans-serif;
}
form {
margin: 10px;
}
input[type=text],
input[type=url],
input[type=email]{
padding : 5px 10px;
border-radius : 10px;
border : 1px solid rgba(1,1,1,0.1);
font-size : 14px;
outline : none;
min-width : 200px;
}
button {
border : 1px solid rgba(1,1,1,0.1);
border-radius : 5px;
background-color : green !important;
color : white;
padding : 10px;
display : block;
font-weight : bold;
text-transform : uppercase;
margin-top : 30px;
min-width : 200px;
}
h4 > i {
font-size : 0.75em;
opacity : 0.25;
}
i.status{
margin : 3px 5px 3px 5px;
font-size : 1.5em;
margin-left : 5px;
display : inline-block;
}
/** HTML5 validation styling. **/
/** ========================== **/
i.status:after {
content : '.';
color : transparent;
}
input:focus:invalid {
background: rgba(255,1,1,0.1);
}
input:focus:invalid + i.status:after{
content : "\2A2F";
color : red;
}
input:not(:invalid):not([type=url]) {
background: rgba(0, 255, 1, 0.1);
}
input:not(:invalid):not([type=url]) + i.status:after {
content : "\2713";
color : green;
}
input:invalid ~ button {
background: black;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment