Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -----------------------------------------
- // Input validation
- // -----------------------------------------
- function validateInput($elm, ignoreEmpty) {
- var value = $elm.val();
- var classTarget = getClassTarget();
- // fail if no value is entered
- // or succeed if empty and should ignore empty
- if (!$elm.val().length) {
- if (ignoreEmpty) { return succeed(); }
- else { return fail(); }
- }
- // fail if this specific type doesn't validate
- var type = $elm.data("validation");
- if (validate.hasOwnProperty(type)
- && !validate[type](value)) { return fail() }
- // everything passed; return true
- return succeed();
- // helper functions
- function fail() {
- classTarget.addClass("group-error");
- $elm[0].valid = false;
- return false;
- }
- function succeed() {
- classTarget.removeClass("group-error");
- $elm[0].valid = true;
- return true;
- }
- function getClassTarget() {
- if ($elm.parent().hasClass("row")) {
- return $elm.parent().parent();
- }
- return $elm.parent();
- }
- };
- // VALIDATORS
- var validate = {
- // Validates a URL
- url: function(str) {
- var urlRegexp = /^(http(?:s)?\:\/\/[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*(?:\.([a-zA-Z0-9]+))?)$/i;
- return urlRegexp.test(str);
- },
- // Validates an image URL
- image: function(str) {
- // if the string isn't a valid URL, fail
- if (!validate.url(str)) { return false; }
- // ensure that the extension is a supported file format
- var supportedFormats = ["jpg", "jpeg", "png"];
- var extension = /.+\/(?:.+)?\.(.+)/.exec(str)[1];
- if (supportedFormats.indexOf(extension) === -1) { return false; }
- return true;
- }
- };
- // Validate inputs when they lose focus
- $("input[data-required], textarea[data-required], input[data-validation]").blur(function(){
- validateInput($(this), true);
- })
- // Validate input when their text is changed
- .on("input", function(){
- var self = this;
- // if it's currently at an error, validate instantly
- if (!this.valid) {
- validateInput($(self), true);
- return;
- }
- // otherwise, wait until the user hasn't typed for 1 sec
- clearTimeout(self.validateTimer);
- self.validateTimer = setTimeout(function(){
- validateInput($(self), true);
- }, 1000);
- });
- // Verify forms
- $('form').each(function() {
- // Variables
- var form = $(this);
- var submit = form.find('input[type="submit"]');
- submit.click(function(e) {
- e.preventDefault();
- // Variables
- var self = $(this);
- var fields = form.find('input[data-required], textarea[data-required]').not('.btn');
- var allFields = form.find('input, textarea').not('.btn');
- var valid = true;
- fields.each(function() {
- if (!validateInput($(this))) { valid = false; }
- });
- // If everything went ok, do certain actions
- if (valid) {
- allFields.val('');
- }
- console.log('Valid: ' + valid);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement