Created
August 23, 2012 00:01
-
-
Save neilberget/3430734 to your computer and use it in GitHub Desktop.
Form Validator for jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FormValidator | |
constructor: (@form, opts) -> | |
{@validations, @human_names} = opts | |
$(@form).submit (e) => | |
@errors = [] | |
for key, value of @validations | |
if typeof(value) == "function" | |
funcs = [value] | |
else | |
funcs = value | |
unless _.all funcs, @runTest(key).bind(@) | |
e.preventDefault() | |
@displayErrors() | |
return | |
runTest: (selector) -> | |
(f) -> | |
f.call(@, selector) | |
humanName: (sel) -> | |
return @human_names[sel] if @human_names[sel] | |
sel.charAt(1).toUpperCase() + sel.substr(2) | |
displayErrors: -> | |
alert @errors[0] | |
# Validation Helpers | |
@required: (sel) -> | |
result = !!$(sel).val() | |
unless result | |
name = @humanName sel | |
@errors.push "#{name} is a required field." | |
result | |
@min: (n) -> | |
(sel) -> | |
result = $(sel).val() && $(sel).val().length >= n | |
unless result | |
name = @humanName sel | |
@errors.push "#{name} must contain at least #{n} characters." | |
result | |
@equals: (other_selector) -> | |
(selector) -> | |
result = $(selector).val() == $(other_selector).val() | |
unless result | |
name1 = @humanName(selector) | |
name2 = @humanName(other_selector) | |
@errors.push "#{name1} must equal #{name2}" | |
result | |
@checked: (sel) -> | |
result = $("#{sel}:checked").length > 0 | |
unless result | |
name = @humanName sel | |
@errors.push "#{name} must be checked." | |
result | |
window.FormValidator = FormValidator | |
window.FV = window.FormValidator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RegisterForm = new FormValidator "#register-form", | |
validations: | |
"#register-username": FV.required | |
"#register-email": FV.required | |
"#password": [FV.min(6), FV.equals("#password-confirm")] | |
"#license-agree": FV.checked | |
human_names: | |
"#register-username": "Username" | |
"#register-email": "Email" | |
"#password-confirm": "Password Confirmation" | |
"#license-agree": "License" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"MVP" form validation library for jQuery (or compatible).
On form submit (1st parameter to FormValidator) each of the functions provided in the validations hash are run against the selectors in the keys, and the first one to fail is alerted.
Human_names is used for prettying up the error message. If not provided, it will capitalize the id. E.g.: "#username" will get printed in the error message as "Username"