-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjs-numeric.js
More file actions
77 lines (63 loc) · 2.32 KB
/
js-numeric.js
File metadata and controls
77 lines (63 loc) · 2.32 KB
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
/*
* js-numeric - 1.01
* Copyright (c) 2013-2015 rchockxm (rchockxm.silver@gmail.com)
*
* Licensed - under the terms of the MIT License
* http://www.opensource.org/licenses/mit-license.php
*/
function NumericLimits(sender, limit_number, callback_func) {
var ret = false;
var user_select = (window.getSelection) ? window.getSelection() : "";
if (typeof sender !== "object") {
return ret;
}
if (user_select == "") {
if (document.getSelection != null && document.getSelection != undefined) {
user_select = document.getSelection();
}
if (user_select == "") {
if (document.selection != null && document.selection != undefined) {
user_select = window.document.selection.createRange().text;
}
}
}
var user_select_pos = sender.value.indexOf(user_select);
var user_force_pos = (sender.selectionStart) ? sender.selectionStart : "";
if (user_force_pos == "" || user_force_pos == null || user_force_pos == undefined) {
if (document.selection) {
sender.focus();
var c_r = document.selection.createRange();
var c_re = sender.createTextRange();
var c_rc = c_re.duplicate();
c_re.moveToBookmark(c_r.getBookmark());
c_rc.setEndPoint("EndToStart", c_re);
user_force_pos = c_rc.text.length;
}
}
var KeyCode = window.event.keyCode;
if (KeyCode >= 48 && KeyCode <= 57) {
var new_value = parseInt(KeyCode - 48);
var old_value = sender.value;
if (user_select != "") {
old_value = sender.value.replace(user_select, "");
}
var global_value = parseFloat(old_value + new_value);
if (global_value <= limit_number) {
ret = true;
}
else {
if (typeof callback_func === "function") {
callback_func(sender, global_value, limit_number);
}
}
}
else {
var old_value = sender.value;
if ((user_select != "" && user_select_pos > 0) || (user_force_pos >= 1)) {
if (KeyCode == 46 && (old_value.split(".").length-1 == 0)) {
ret = true;
}
}
}
return ret;
}