1(function ($) { 2 3 var regEmpty = /^\s*$/; 4 var regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 5 var errors = { 6 "cant-be-empty": "This field is required", 7 "must-be-email": "Please enter a valid email address" 8 }; 9 10 function validateField($fld, noFocus) { 11 var val = $fld.val(); 12 13 hideErrors($fld); 14 15 if ($fld.is('[required]') && regEmpty.test(val)) { 16 setTimeout(function () { 17 showError($fld, "cant-be-empty", noFocus); 18 }, 0); 19 return false; 20 } 21 22 if ($fld.is('[type="email"]') && !regEmail.test(val)) { 23 showError($fld, "must-be-email", noFocus); 24 return false; 25 } 26 27 return true; 28 } 29 30 function hideErrors($fld) { 31 $fld.removeClass('invalid'); 32 } 33 34 function showError($fld, msgClass, noFocus) { 35 var form = $fld[0].form; 36 37 var msg = $fld.attr("data-" + msgClass); 38 if (!msg) { 39 msg = errors[msgClass]; 40 } 41 42 $fld.addClass('invalid'); 43 44 label = $(".error-message", form); 45 if (!label.length) { 46 label = $('<div class="error-message"/>'); 47 } 48 49 label = label.stop(1, 1).css({ 50 left: 0, 51 opacity: 0 52 }).text(msg).appendTo($fld.closest(".form-field-wrapper")); 53 54 label.animate({ 55 left: ["100%", "easeOutExpo"], 56 opacity: [1, "linear"] 57 }, 500); 58 59 if (!noFocus) { 60 $fld.trigger("focus"); 61 } 62 } 63 64 // Plugin 65 $.fn.validator = function () { 66 return this.filter("form").each(function (i, f) { 67 68 // turn off the native validation if any 69 f.noValidate = true; 70 71 $(f.elements).not(":button, :checkbox, :radio, :submit, :reset, :hidden, fieldset, label, form").each(function () { 72 $fld = $(this); 73 74 var fld_width = $fld.outerWidth({margin: true}); 75 var fld_inner_width = $fld.outerWidth(); 76 $fld.wrap( $('<div class="form-field-wrapper"/>').css({ width: fld_width }) ).css({ 77 width: fld_inner_width 78 }); 79 80 $fld.unbind("change.validateForm keyup").bind("change.validateForm keyup", function (e) { 81 if (e.type == "keyup" && e.keyCode == 9) { 82 return true; 83 } 84 $fld.toggleClass("invalid", !validateField($(this), true)); 85 }); 86 }); 87 88 f.validate = function (noFocus) { 89 $(".error-message", this).css({ 90 opacity: 0 91 }); 92 for (var j = 0; j < this.elements.length; j++) { 93 if (!validateField($(this.elements[j]), noFocus)) { 94 return false; 95 } 96 } 97 return true; 98 }; 99 100 $(f).unbind("submit.validateForm").bind("submit.validateForm", function () { 101 return f.validate(); 102 }).unbind("reset.validateForm").bind("reset.validateForm", function () { 103 $(this.elements).removeClass("invalid"); 104 }); 105 }); 106 }; 107 108})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.