-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequiredfield.js
executable file
·133 lines (109 loc) · 2.76 KB
/
requiredfield.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
/*
* requiredfield jquery widget
*
* Copyright 2011, Acatl Pacheco
* Licensed under the MIT License.
*
*/
$.widget("ui.requiredfield", {
options : {
isRequired : true,
requiredClass : "required",
watermarkText : null,
watermarkClass : "watermark",
functionValidate : null,
dataType : "string",
liveCheck : false,
leaveWatermark:false,
defaultIsInvalid:false
},
_create : function() {
this.element.focus($.proxy(this._focusHandler, this));
this.element.blur($.proxy(this._blurHandler, this));
if (this.options.liveCheck) {
this.element.keyup($.proxy(this._keyupHandler, this));
}
},
_init : function() {
if (this.options.watermarkText != null) {
this._updateWatermark();
}
},
_focusHandler : function(e) {
var value = this.element.val();
if (value == this.options.watermarkText && !this.options.leaveWatermark) {
this.element.val("");
}
this.element.toggleClass(this.options.watermarkClass, false);
if ( this.options.leaveWatermark ) {
}
},
_blurHandler : function(e) {
this._updateWatermark();
this._updateValidation();
},
_keyupHandler : function(e) {
this._updateValidation();
},
_updateValidation : function() {
this.validate();
},
_updateWatermark : function() {
if (this.options.watermarkText == null)
return;
var value = this.element.val();
var watermarkIt = false;
if (value == "" || value == this.options.watermarkText) {
watermarkIt = true;
this.element.val(this.options.watermarkText);
}
this.element.toggleClass(this.options.watermarkClass, watermarkIt);
},
refresh: function () {
if (arguments[0] == true) {
this.element.val("");
}
if (this.options.watermarkText != null) {
this._updateWatermark();
}
this.element.toggleClass(this.options.requiredClass, false);
},
validate : function() {
var valid = true;
if (this.options.isRequired) {
valid = this.isValid();
}
this.element.toggleClass(this.options.requiredClass, !valid);
return valid;
},
isValid : function() {
var value = this.element.val();
var valid = true;
if (value == "") {
return false;
}
if (this.options.defaultIsInvalid == true && value == this.options.watermarkText) {
return false;
}
var dataTypeValid = true;
if (this.options.dataType != null) {
var val = null;
switch (this.options.dataType) {
case "number":
dataTypeValid = !isNaN(Number(value));
break;
case "string":
default:
dataTypeValid = jQuery.type(value) == this.options.dataType;
break;
}
if (!dataTypeValid)
return false;
}
var functionValidateValid = true;
if (this.options.functionValidate != null) {
functionValidateValid = this.options.functionValidate(this.element.val());
}
return dataTypeValid && functionValidateValid;
}
});