-
Notifications
You must be signed in to change notification settings - Fork 5
/
jquery.formError.js
175 lines (155 loc) · 5.78 KB
/
jquery.formError.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// jQuery.formError v0.3
//
// Copyright (C)2012 Gareth Elms
// Distributed under MIT License
//
// Documentation and full license availabe at:
// https://github.com/GarethElms/jQuery.formError
(function( $ )
{
$.fn.formError =
function( message, options)
{
if( typeof( message) === "string")
{
options = $.extend( true, {}, $.fn.formError.defaultOptions, options);
options.message = message;
show.call( this, options);
}
else
{
var options = $.extend( true, {}, $.fn.formError.defaultOptions, message);
if( typeof( options.message) === "string")
{
show.call( this, options);
}
else if( options.remove)
{
if( remove.call( this, options))
{
this.addClass( "valid");
}
}
}
function remove( options)
{
var wasInErrorState = false;
if( options.successImage.enabled)
{
if( this.hasClass('invalid')) //Used to be invalid
{
var img = $( "<img class='successImage' style='position:absolute; right:-20px; top:3px; z-index:9999;' src='" + options.successImage.src + "' />");
var positionMethod = this.css( "position");
if( positionMethod == "relative" || positionMethod == "absolute" || positionMethod == "fixed")
{
img
.css( "left", ((this.position().left + this.outerWidth()) + 3) + "px")
.css( "top", (this.position().top - 3) + "px");
}
else
{
img
.css( "left", ((this.position().left + this.outerWidth()) + 5) + "px")
}
this.after( img.fadeIn());
}
else
{
removeSuccessImage.call( this);
}
}
if( this.hasClass( "invalid"))
{
this.removeClass('invalid');
wasInErrorState = true;
}
this.next(".validationErrorContainer").fadeOut();
this.next("img.successImage").next( ".validationErrorContainer").fadeOut();
return wasInErrorState;
}
function removeSuccessImage()
{
if( this.next().hasClass( "successImage"))
{
this.next().fadeOut();
}
}
function show( options)
{
remove.call( this, {successImage: {disabled:true}}); // Just remove the previous error message if it exists, we are replacing it now
removeSuccessImage.call( this); // Also remove the success image if present
options.message = options.message.injectNewLines( options.newLineAtCharacterCount);
var errorDiv =
$("<div class='validationErrorContainer' style='position:absolute; left:101%; top:-2px; z-index:99999;'>" +
"<canvas width='14' height='14' style='position:absolute; left:-3px; top:7px;' />" +
"<div class='validationError' style='border:2px solid #811; border-radius:5px; padding:4px; background-color:#f99; color:#511; position:relative; left:10px; white-space:nowrap;'>" +
options.message +
"</div>" +
"</div>");
var positionMethod = this.css( "position");
if( positionMethod == "relative" || positionMethod == "absolute" || positionMethod == "fixed")
{
errorDiv
.css( "left", ((this.position().left + this.outerWidth()) - 3) + "px")
.css( "top", (this.position().top - 3) + "px");
}
else if( this.parent().hasClass( "inputContainer") == false)
{
this.wrap( $("<div class='inputContainer' style='position:relative; width:" + this.width() + "px'></div>"));
}
this.after( errorDiv.fadeIn()).addClass( "invalid");
var canvas = $("canvas", errorDiv)[0];
if( typeof( canvas.getContext) == "function")
{
var context = canvas.getContext( "2d");
context.fillStyle = '#811';
context.strokeStyle = '#811';
context.lineWidth = 1;
context.beginPath();
context.moveTo(1, 7);
context.lineTo(13, 13);
context.lineTo(13, 1);
context.lineTo(1, 7);
context.closePath();
context.fill();
context.stroke();
}
}
};
$.fn.formError.defaultOptions =
{
newLineAtCharacterCount: 30,
successImage:
{
enabled:true,
src: "success.gif"
}
};
})( jQuery );
String.prototype.injectNewLines =
function( maxLineLength)
{
if( typeof( maxLineLength) == "number" && maxLineLength > 0)
{
var tempMessage = "";
var lineLength = 0;
var words = this.split( /\s+/);
for( var word in words)
{
tempMessage += words[word];
lineLength += words[word].length;
if( lineLength > maxLineLength)
{
tempMessage += "<br />";
lineLength = 0;
}
else
{
tempMessage += " ";
lineLength ++;
}
}
return tempMessage;
}
return this;
};