$(document).ready(function(){
var $inputs = $('input[type="text"], select, textarea', '#form_builder').css('z-index', 5); var errors = Object();
$inputs.focusin(function(){ var $this = $(this); $this.removeClass('error'); $this.parent().find('.alert').slideDown(); });
$inputs.focusout(function(){ var $this = $(this); $this.parent().find('.alert').slideUp(); });
$inputs.keyup(function(){ var $this = $(this); var id = $this.attr('id'); if (!id || !(id in errors) || !('test' in errors[id])) return; var value = $.trim($this.val()); var testFunction = errors[id].test; if (!isFunction(testFunction) || testFunction(value)) { $this.removeClass('error').parent().removeClass('has-error'); $this.parent().find('.alert').slideUp('slow', function () { $(this).remove(); }); }
});
$('#form_builder').submit(function(e){ e.preventDefault(); $('div.has-error').removeClass('has-error'); var form = this; var $fields = $('input, select, textarea', $(this)); var found_errors = false; var msg = ''; var testFunction = null;
resetErrors();
for(var i = 0; i < $fields.length; i++){ var $field = $($fields[i]); var value = $.trim($field.val()); //$field.val(value); if (isRequired($field) && !value){ // If fails to "Is Required" test if ($field.data('requiredMessage')) msg = $field.data('requiredMessage'); else msg = 'This field is required!'; errors[$field.attr('id')] = { 'msg': msg, 'test': isNonEmpty }; createErrorMessage($field, errors[$field.attr('id')]); $field.addClass('error').parent().addClass('has-error'); found_errors = true; continue; } if (isUrl($field) && isNonEmpty(value) && !isValidUrl(value)){ msg = 'Invalid URL format'; errors[$field.attr('id')] = { 'msg': msg, 'test': isValidUrl }; createErrorMessage($field, errors[$field.attr('id')]); $field.addClass('error').parent().addClass('has-error'); found_errors = true; continue; } if (isEmail($field) && isNonEmpty(value) && !isValidEmail(value)){ msg = 'Invalid Email format'; errors[$field.attr('id')] = { 'msg': msg, 'test': isValidEmail }; createErrorMessage($field, errors[$field.attr('id')]); $field.addClass('error').parent().addClass('has-error'); found_errors = true; continue; } } if (!found_errors) form.submit(); }); function isRequired($field){ return $field.closest('div.wl-form-builder').hasClass('required') || $field.hasClass('required'); } function isUrl($field){ return $field.hasClass('validate-url'); } function isEmail($field){ return $field.hasClass('validate-email'); } function isNonEmpty(str){ if (str) return true; return false; } function isValidUrl(str){ var myRegExp =/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i; return myRegExp.test(str) || myRegExp.test('http://' + str); } function isValidEmail(str){ var myRegExp =/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/; return myRegExp.test(str); } function resetErrors(){ for(var i in errors){ errors[i] = {}; } } function createErrorMessage($field, error_data){ var str = '
'; str = $(str); $field.parent().css('position','relative').append(str); }
function isFunction(functionToCheck) { var getType = {}; return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; }
});