Skip to content

Instantly share code, notes, and snippets.

@jdaigle
Created July 14, 2011 22:20
Show Gist options
  • Save jdaigle/1083608 to your computer and use it in GitHub Desktop.
Save jdaigle/1083608 to your computer and use it in GitHub Desktop.
No Frills JQuery/HTML5 Validation
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
if (typeof (nfvalidate) == "undefined") nfvalidate = {};
// An array of validators to push your validators to
nfvalidate.validators = new Array();
nfvalidate.validators.push({
name: "validation-required",
doValidate: function(e) { return !$(e).data("validation-required") || $(e).val() != ''; },
errorMessage: function(e) { return "Field is required"; }
});
nfvalidate.validators.push({
name: "validation-maxlength",
doValidate: function(e) {
return !($(e).val().length > this.getMaxLength(e));
},
errorMessage: function(e) { return "Field cannot be more than " + this.getMaxLength(e) + " characters"; },
getMaxLength: function(e) { return parseInt($(e).data("validation-maxlength")); }
});
// Finds an Error Message data value or undefined
nfvalidate.findErrorMessage = function(element, validator) {
return $(element).data(validator.name + "-message");
}
// Returns an array of validation errors for the specific element
nfvalidate.validateElement = function(element) {
var errors = new Array();
$.each(nfvalidate.validators, function(i, validator) {
if ($(element).data(validator.name) === undefined) return;
if (validator.doValidate(element) == false) {
var errorMessage = nfvalidate.findErrorMessage(element, validator);
if (errorMessage === undefined) errorMessage = validator.errorMessage(element);
errors.push({ element: element, message: errorMessage });
}
});
return errors;
};
// Finds all elements that need validation within the selected element (such as a form) and
// returns an array of all validation errors
nfvalidate.validate = function(selector) {
var validationSelectors = $.map(nfvalidate.validators, function(e, i) { return "[data-"+ e.name + "]"; }).join(",");
var elementsToValidate = $(selector).find(validationSelectors);
return $.map(elementsToValidate, function(e, i) { return nfvalidate.validateElement(e); });
};
$(function() {
$("#myForm").submit(function(e) {
var errors = nfvalidate.validate(this);
for (i in errors) {
alert(errors[i].message);
}
if (errors.length > 0) e.preventDefault();
});
});
</script>
</head>
<body>
<form id="myForm">
Required: <input type="text" data-validation-required="true" /><br />
Not Required: <input type="text" data-validation-required="false" /><br />
Max Length: <input type="text" data-validation-maxlength="50" /><br />
Required: <input type="text" data-validation-required="true" data-validation-required-message="my field is required" /><br />
Max length: <input type="text" data-validation-maxlength="60" data-validate-maxlength-message="cannot be more than 60 chars!!" /><br />
Max Length And Required: <input type="text" data-validation-required="true" data-validation-maxlength="50" /><br />
<input type="submit" value="do submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment