-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.prettyMailInput.js
103 lines (87 loc) · 3.51 KB
/
jquery.prettyMailInput.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
(function ($, w, d) {
var defaults = {
theme: "blue"
}
var methods= {
init: function(options)
{
return this.each(function () {
var elem = $(this);
var settings = $.extend(defaults,options);
if (elem.is("div")) {
elem.attr("contentEditable", "true");
if (settings.theme == "blue")
elem.addClass("box bluebox");
else if(settings.theme=="purple")
elem.addClass("box purplebox");
//This is to prevent line break in input.
elem.on("keypress", function (e) {
if(e.which == 13 || e.which == 32) //enter and space key prevent
return false;
})
elem.on("keyup", function (e) {
if (e.which == 13) {
e.preventDefault();
var input = elem.clone().children().remove().end().text();
CallEmailHandler(elem, input.trim());
}
});
} else {
$.error("prettyMailInput() is only applicable to a 'div' element");
}
});
},
getValue: function()
{
return $(this).text().split(";");
}
};
$.fn.prettyMailInput = function (methodOrOptions) {
if(methods[methodOrOptions])
return methods[methodOrOptions].apply(this)
else if(typeof methodOrOptions === "object" || !methodOrOptions)
return methods.init.apply(this,arguments);
}
var CallEmailHandler = function (elem, inputVal) {
if (inputVal === "" || inputVal === {}) {
elem.addClass("error");
return false;
}
if (!Validate(inputVal)) {
elem.addClass("error");
return false;
}
elem.removeClass("error");
var $span = $("<a />");
$span.addClass("mailInput").attr("contentEditable", false).text(inputVal);
/* Remove the text inside input except span elements */
elem.contents().filter(function () {
return this.nodeType == 3
}).each(function () {
this.textContent = "";
});
elem.append($span).append(" ");
placeCaretAtEnd(elem);
}
var Validate = function (email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
var placeCaretAtEnd = function (el) {
if (typeof window.getSelection != "undefined" &&
typeof document.createRange != "undefined") {
var range = document.createRange();
range.setStartAfter(el.children().get(-1));
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
setTimeout(function(){el.get(0).focus();});
}
}(jQuery, window, document));