Ideal Forms is the ultimate framework for building and validating responsive HTML5 forms.
- Fully responsive (AKA adaptive, adapts to the container, no css media queries needed).
- Keyboard support.
- Every input type can be customized including
select
,radio
,checkbox
andfile
. - Custom datepicker using jQuery UI (with fallback)
- "On the spot" validation.
- Localization
- HTML5
placeholder
for every browser.
- New
range
filter. - New
dob
Date of Birth filter. - New tabs(steps) with new public methods
switchTab
,nextTab
,prevTab
,firstTab
andlastTab
. - New custom datepicker (requires jQuery UI)
"Help translating Ideal Forms into other languages."
"I need feedback for mobile devices. Please report any bugs you might find in iOS or Android."
Load the latest jQuery library, the js/min/jquery.idealforms.min.js
plugin and the css/jquery.idealforms.css
stylesheet into your project. Load the latest jQuery UI if you want support for datepicker.
Replace your document's opening <html>
tag with the following conditional comments. This will load the appropiate fixes for all supported IE versions:
<!--[if IE 8]> <html class="ie8" lang="en"> <![endif]-->
<!--[if IE 9]> <html class="ie9" lang="en"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en"> <!--<![endif]-->
Also, for IE8 support you'll need an HTML5 shim unless you're using Modernizr which already has a shim.
To localize Ideal Forms in your language, load the corresponding file from js/i18n
after loading the plugin.
For Ideal Forms to work its magic create your markup using the following template as a reference, nothing fancy, just the usual form tags wrapped in a <div>
. Drop the form into a container of any size and Ideal Forms will do the rest.
If you're working with dynamic fields check documentation on addFields.
<form id="my-form">
<!-- TAB -->
<section name="First tab">
<!-- Heading -->
<div>
<h1>My Heading</h1>
<p>Description here</p>
</div>
<!-- Text -->
<div><label>Username:</label><input type="text" name="username"/></div>
<div><label>Date:</label><input type="text" name="date" class="datepicker" placeholder="mm/dd/yy"/></div>
<div><label>Comments:</label><textarea name="comments"></textarea></div>
<!-- File -->
<div><label>File Upload:</label><input type="file" multiple name="file"/></div>
<!-- Select -->
<div>
<label>Colors:</label>
<select name="colors">
<option value="default">Choose a color</option>
<option value="Red">Red</option>
<option value="Blue">Blue</option>
<option value="Green">Green</option>
</select>
</div>
<section> <!-- END TAB -->
<!-- TAB -->
<section name="Second tab">
<div>
<h1>My Heading</h1>
<p>Description here</p>
</div>
<!-- Checkbox -->
<div>
<label>Languages:</label>
<label><input type="checkbox" name="langs[]" value="English"/>English</label>
<label><input type="checkbox" name="langs[]" value="Chinese"/>Chinese</label>
<label><input type="checkbox" name="langs[]" value="Spanish"/>Spanish</label>
</div>
<!-- Radio -->
<div>
<label>Options:</label>
<label><input type="radio" name="options" value="One"/>One</label>
<label><input type="radio" name="options" value="Two"/>Two</label>
<label><input type="radio" name="options" value="Three"/>Three</label>
</div>
<section> <!-- END TAB -->
<!-- Separator -->
<div><hr/></div>
<!-- Buttons -->
<div><input type="sumbit" value="Submit"/></div>
<div><button>Reset</button></div>
</form>
The name
attribute will be used in the plugin's options to add filters to each input. This provides a lot of flexibility and the possibility to create custom errors, and tweak the filter's values.
Alternatively, for very simple forms, you can do it "the easy way" and just add the filters as classes.
<form id="my-form">
<div><label>Username:</label><input type="text" name="username" class="required username"/></div>
<div><label>Password:</label><input type="text" name="password" class="required password"/></div>
<div><label>E-Mail:</label><input type="text" name="email" class="required email"/></div>
</form>
Call Ideal Forms on each form separately. Assign each form to a variable to have access to the public methods.
var $myform = $('#my-form').idealforms({ options });
####inputs
Add all the inputs you want to validate here. Use the name attribute of the input as key. To be consistent always put the key in quotes. Array group names can be used too, ie. name[]
.
Each input can be customized with filters, data, errors and flags.
filters
: A space separated string of filters.data
: Filters that take values can be specified in here. Check documentation on Built-in filters for more info.errors
: Use the filter name as the key value and add your custom error. You can use inline HTML tags within the error string.flags
: Flags are simply functions that run when an input tries to validate. See documentation on Built-in flags and customFlags.
inputs: {
// The name attribute of the input in quotes
'myinput': {
filters: 'required min',
data: {
min: 10
},
errors: {
min: 'At least 10 characters'
},
flags: 'noclass noinvalidicon'
}
}
####customFilters
You can add custom filters when invoking the plugin:
customFilters: {
custom: {
regex: /regularexpression/,
error: 'My custom error'
}
}
Or you can add them to the global Ideal Forms filters (recommended):
$.extend($.idealforms.filters, {
another: function (input, value) {
var $input = input.input,
userOptions = input.userOptions
this.error = 'Something ' + value
}
})
####customFlags
Add custom flags:
customFlags: {
/*
* @param input jQuery input object
* @param event The event that was triggered on the input (focus, blur, change, keyup)
*/
custom: function(input, event){
if (event === 'keyup') console.log(input.val())
}
}
####globalFlags
List the flags that you want to apply to all inputs.
globalFlags: 'noerror noicons'
####onSuccess
onSuccess: function(e){
// Form validates
}
####onFail
onFail: function(){
// Form does NOT validate
}
####responsiveAt
By default, Ideal Forms will make the form "adaptive". It will adapt to the container allowing it to work with any responsive grid system.
You can change this behavior by assigning a number value to the responsiveAt
option.
// Make responsive only at a certain window size.
// Default is `"auto"` to adapt to the container
// Set to `false` to disable responsiveness
// To always show the responsive layout use a large number ie `3000`
responsiveAt: 480
####disableCustom
Disables custom inputs and uses system default.
disableCustom: 'file select radiocheck button'
You may use any of these filters in any order.
####required
The field is required. This filter ONLY works with text inputs (text, password, textarea). For select
use exclude
to exclude the default option. For radio
and checkbox
use min: 1
which will require at least one option to be checked.
####number
Must be a number.
####digits
Only digits.
####range
Only numbers within a range. Usually combined with number
or digits
.
'myinput': {
filters: 'number range',
data: {
range: [1, 100]
}
}
####name
Must be at least 3 characters long, and must only contain letters.
####username
Must be between 4 and 32 characters long and start with a letter. You may use letters, numbers, underscores, and one dot (.)
####pass
Must be at least 6 characters long, and contain at least one number, one uppercase and one lowercase letter.
####strongpass
Must be at least 8 characters long and contain at least one uppercase and one lowercase letter and one number or special character.
####email
Must be a valid e-mail address.
####phone
Must be a valid US phone number.
####zip
Must be a valid US zip code.
####url
Must be a valid URL.
####date
Must be a valid date. This filter effectively validates a date, so stuff like 02-31-2012
or 30/80/2000
would be invalid. You can use any format with 4 digit year and any delimiter character. The default format is mm/dd/yyyy
.
'myinput': {
filters: 'date',
data: {
date: 'dd-mm-yyyy' // or `yyyy~dd~mm` or `mm*yyyy*dd`...
}
}
To use the datepicker you need to load jQuery UI and add the class datepicker
to your date input. Ideal Forms will apply the custom format that you specify without having to configure the datepicker. It's seamless.
####dob
Must be a valid date of birth in this century, that is 100 years range from the current year.
'myinput': {
filters: 'dob',
data: {
dob: 'yyyy/dd/mm'
}
}
####min
- Must be at least
x
characters minimum. - Must have at least
x
checkboxes checked.
'myinput': {
filters: 'min',
data: {
min: 10
}
}
####max
x
characters maximum.- No more than
x
checkboxes checked.
'myinput': {
filters: 'max',
data: {
max: 10
}
}
####exclude
- Prevent validation if the value matches any value in the given array.
- Use this filter to exclude the default (usually first) option of a
select
input.
'myinput': {
filters: 'exclude',
data: {
// Always an array even if just one value
// Values are case-sensitive
exclude: ['one', 'two', 'three']
}
}
####equalto
The value must match a value of another input.
'myinput': {
filters: 'equalto',
data: {
// You can use any valid jQuery selector
equalto: '#myid'
}
}
####extension
This filter is designed for file
inputs. It supports multifile in HTML5 browsers.
'myinput': {
filters: 'extension',
data: {
extension: ['jpg', 'png'] // Always array even if just one
}
}
noerror
: hide the error from the inputnoicons
: hide the iconsnovalidicon
noinvalidicon
noclass
: no valid/invalid classnovalidclass
noinvalidclass
####isValid
Check if the form is valid.
chainable: no
if ($myform.isValid()) {
// do something...
}
####addFields
Add fields to the form dynamically. It takes an array of objects. Ideal Forms auto-generates the markup for inputs fields based on the type
specified.
chainable: yes
#####name
(required)
The value used as name attribute.
#####label
(required)
The label text.
#####type
(required)
text
, password
, email
, number
, url
,tel
, file
, select
, radio
and checkbox
.
#####addBefore
Insert the new field before an existing field. Takes a string. Ideal Forms will look for name
first and then id
.
#####addAfter
Insert the new field after an existing field.
#####appendToTab
Insert the new field at the end of a section. Takes a string.
#####list
Array of items for inputs such as select
, radio
and checkbox
.
#####placeholder
The placeholder for text inputs. Use exclude
for select menus.
#####filters
, data
, errors
, flags
For more info check documentation on input's options.
var newFields = [
{
name: 'animals[]',
label: 'Animals',
type: 'radio',
list: ['Dog', 'Elephant', 'Crocodile', 'Spider'],
addAfter: 'email'
},
{
name: 'zip',
label: 'Zip Code',
filters: 'required zip',
type: 'text',
addBefore: 'password'
},
{
name: 'instruments',
label: 'Instruments',
filters: 'exclude',
data: { exclude: ['Select an instrument'] },
errors: { exclude: 'Please select an instrument' },
type: 'select',
list: [
'Select an instrument',
'Piano',
'Violin',
'Guitar'
],
appendToTab: 'My Section'
}
]
$myform.addFields(newFields)
####removeFields
Remove fields from the form dynamically. It takes an array of names or id's. Ideal Forms will look for the name
attribute first and then id
.
chainable: yes
var fields = [
'username',
'password',
'email'
]
$myform.removeFields(fields)
####isValidField
Check if a particular field is valid. The function takes a string. Ideal Forms will look for the name attribute first and then for the id. You can use array group names for groups of checkboxes.
chainable: no
if ($myform.isValidField('username')) { // name="username" OR #username
// do something...
}
if ($myform.isValidField('colors[]')) { // name="colors[]"
// do something...
}
####getInvalid
Get all invalid fields. Returns a jQuery object. You can also filter by tab name to get all invalid fields within a tab.
chainable: yes (but it doesn't return the form, just the invalid fields)
var numInvalid = $myform.getInvalid().length // How many invalid fields
var numInvalidTab = $myform.getInvalid('My Section').length
####focusFirst
Focus the very first field.
chainable: yes
$myform.focusFirst()
####focusFirstInvalid
Focus the first invalid field.
chainable: yes
$myform.focusFirstInvalid()
####switchTab
Change tab by name.
chainable: yes
$myform.switchTab('My Section')
####nextTab
Go to next tab.
chainable: yes
$myform.nextTab()
####prevTab
Go to previous tab.
chainable: yes
$myform.prevTab()
####firstTab
Go to first tab.
chainable: yes
$myform.firstTab()
####lastTab
Go to last tab.
chainable: yes
$myform.lastTab()
####fresh
Load the form as if it was never focused. This removes valid
and invalid
classes until first focus.
chainable: yes
$myform.reset().fresh() // Usually combined with `reset()`
####reset
Reset all fields to zero including checkboxes, radios, and selects. You can also reset a particular field by passing in the field name or id.
chainable: yes
$myform.reset() // Reset all
$myform.reset('password') // Reset password field
With the markup provided above you'd call the plugin like this:
$myform.idealforms({
inputs: {
'username': {
filters: 'required username exclude',
data: {
exclude: ['user', 'username', 'admin']
}
},
'date': {
filters: 'date'
},
'comments': {
filters: 'min max',
data: {
min: 50
max: 200
}
},
'colors': {
filters: 'exclude',
data: {
exclude: ['default']
},
errors: {
exclude: 'Choose a color from the list.'
}
},
'langs[]': {
filters: 'min',
data: {
min: 2
},
errors: {
min: 'Check at least <strong>2</strong> languages.'
}
},
'options': {
filters: 'min'
data: {
min: 1
},
errors: {
min: 'Check only <strong>1</strong> option.'
}
}
}
});
Ideal Forms relays on a carefully crafted LESS stylesheet. Everything is customizable, from the simplest text input, to the select menus, radios, and checkboxes.
Here's a list of all the Ideal Forms UI elements that can be customized:
- Tabs
- Labels
- Headings
- Separators
- Icons
- Errors
- Text inputs
- Buttons
- Select dropdowns
- Radios
- Checkboxes
- Datepicker
Really, ANYTHING.
All user options are located in less/themes/theme/theme.less
. You can safely edit all values from the "user config". The "extra" options must be edited wisely since most of the variables here are relative to other variables defined elsewhere.
The names of the user config variables are pretty self-explanatory. If you screw up you can always go back.
When you finish editing the user config don't forget to load your theme in less/jquery.idealforms.less
and compile into css
.
####@font-size
The overall font size. Usually adjusting this option should be enough in most cases. Keep in mind that the bigger the font, the bigger the icons need to be. Ideal Forms will try to align everything as close as possible, but it's recommended that the icons are aprox. the same size as the font-size.
####@small-font-size
Ideal Forms uses a smaller font-size for button
, select
, radio
, checkbox
and file
. This makes UI elements stand out more and feel more integrated. Change to 100%
to use the default font-size.
####@input-width
Adjust this option if the form doesn't quite fit your container. It's recommended to use this format @font-size * number
.
All inputs will have the width set here but you can change the width of any particular input by targeting its id. This won't affect the responsive layout.
#comments { width: 200px; }
####@label-align
Align labels to either left
or right
.
####@label-width
Most of the time auto
will work just fine but if you have really long label names then it's a good idea to tweak this value.
####@border-width
Width of every border property. Usually there's no need to change the default value of 1px
unless you want a "thick" border look, suitable for some occasions. box-shadow
properties depend on this option.
####@border-radius
Radius for round corners.
####@css3-anim
true
or false
to enable or disable css3 animations (transitions).
####@css3-effects
true
or false
to enable or disable css3 box-shadow and gradients.
Enjoy :)