PageSourceSearch

https://ew.smilebpi.com/einfo/Content/Scripts/jquery.numeric.js

js smilebpi.com collected 2026-10-05 23:52:13 UTC 8,509 bytes, 280 lines download raw bytes

vendor: 4,757 bytes, lines 1-128
1/*
2 *
3 * Copyright (c) 2006-2011 Sam Collett (http://www.texotela.co.uk)
4 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
5 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
6 * 
7 * Version 1.3.1
8 * Demo: http://www.texotela.co.uk/code/jquery/numeric/
9 *
10 */
11(function($) {
12/*
13 * Allows only valid characters to be entered into input boxes.
14 * Note: fixes value when pasting via Ctrl+V, but not when using the mouse to paste
15  *      side-effect: Ctrl+A does not work, though you can still use the mouse to select (or double-click to select all)
16 *
17 * @name     numeric
18 * @param    config      { decimal : "." , negative : true }
19 * @param    callback     A function that runs if the number is not valid (fires onblur)
20 * @author   Sam Collett (http://www.texotela.co.uk)
21 * @example  $(".numeric").numeric();
22 * @example  $(".numeric").numeric(","); // use , as separator
23 * @example  $(".numeric").numeric({ decimal : "," }); // use , as separator
24 * @example  $(".numeric").numeric({ negative : false }); // do not allow negative values
25 * @example  $(".numeric").numeric(null, callback); // use default values, pass on the 'callback' function
26 *
27 */
28$.fn.numeric = function(config, callback)
29{
30	if(typeof config === 'boolean')
31	{
32		config = { decimal: config };
33	}
34	config = config || {};
35	// if config.negative undefined, set to true (default is to allow negative numbers)
36	if(typeof config.negative == "undefined") { config.negative = true; }
37	// set decimal point
38	var decimal = (config.decimal === false) ? "" : config.decimal || ".";
39	// allow negatives
40	var negative = (config.negative === true) ? true : false;
41	// callback function
42	callback = (typeof(callback) == "function" ? callback : function() {});
43	// set data and methods
44	return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.callback", callback).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur);
45};
46
47$.fn.numeric.keypress = function(e)
48{
49	// get decimal character and determine if negatives are allowed
50	var decimal = $.data(this, "numeric.decimal");
51	var negative = $.data(this, "numeric.negative");
52	// get the key that was pressed
53	var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
54	// allow enter/return key (only when in an input box)
55	if(key == 13 && this.nodeName.toLowerCase() == "input")
56	{
57		return true;
58	}
59	else if(key == 13)
60	{
61		return false;
62	}
63	var allow = false;
64	// allow Ctrl+A
65	if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) { return true; }
66	// allow Ctrl+X (cut)
67	if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) { return true; }
68	// allow Ctrl+C (copy)
69	if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) { return true; }
70	// allow Ctrl+Z (undo)
71	if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) { return true; }
72	// allow or deny Ctrl+V (paste), Shift+Ins
73	if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */ ||
74	  (e.shiftKey && key == 45)) { return true; }
75	// if a number was not pressed
76	if(key < 48 || key > 57)
77	{
78	  var value = $(this).val();
79		/* '-' only allowed at start and if negative numbers allowed */
80		if(value.indexOf("-") !== 0 && negative && key == 45 && (value.length === 0 || parseInt($.fn.getSelectionStart(this), 10) === 0)) { return true; }
81		/* only one decimal separator allowed */
82		if(decimal && key == decimal.charCodeAt(0) && value.indexOf(decimal) != -1)
83		{
84			allow = false;
85		}
86		// check for other keys that have special purposes
87		if(
88			key != 8 /* backspace */ &&
89			key != 9 /* tab */ &&
90			key != 13 /* enter */ &&
91			key != 35 /* end */ &&
92			key != 36 /* home */ &&
93			key != 37 /* left */ &&
94			key != 39 /* right */ &&
95			key != 46 /* del */
96		)
97		{
98			allow = false;
99		}
100		else
101		{
102			// for detecting special keys (listed above)
103			// IE does not support 'charCode' and ignores them in keypress anyway
104			if(typeof e.charCode != "undefined")
105			{
106				// special keys have 'keyCode' and 'which' the same (e.g. backspace)
107				if(e.keyCode == e.which && e.which !== 0)
108				{
109					allow = true;
110					// . and delete share the same code, don't allow . (will be set to true later if it is the decimal point)
111					if(e.which == 46) { allow = false; }
112				}
113				// or keyCode != 0 and 'charCode'/'which' = 0
114				else if(e.keyCode !== 0 && e.charCode === 0 && e.which === 0)
115				{
116					allow = true;
117				}
118			}
119		}
120		// if key pressed is the decimal and it is not already in the field
121		if(decimal && key == decimal.charCodeAt(0))
122		{
123			if(value.indexOf(decimal) == -1)
124			{
125				allow = true;
126			}
127			else
128			{
129				allow = false;
130			}
131		}
132	}
133	else
134	{
135		allow = true;
136	}
137	return allow;
138};
139
140$.fn.numeric.keyup = function(e)
141{
142	var val = $(this).value;
143	if(val && val.length > 0)
144	{
145		// get carat (cursor) position
146		var carat = $.fn.getSelectionStart(this);
147		// get decimal character and determine if negatives are allowed
148		var decimal = $.data(this, "numeric.decimal");
149		var negative = $.data(this, "numeric.negative");
150
151		// prepend a 0 if necessary
152		if(decimal !== "" && decimal !== null)
153		{
154			// find decimal point
155			var dot = val.indexOf(decimal);
156			// if dot at start, add 0 before
157			if(dot === 0)
158			{
159				this.value = "0" + val;
160			}
161			// if dot at position 1, check if there is a - symbol before it
162			if(dot == 1 && val.charAt(0) == "-")
163			{
164				this.value = "-0" + val.substring(1);
165			}
166			val = this.value;
167		}
168
169		// if pasted in, only allow the following characters
170		var validChars = [0,1,2,3,4,5,6,7,8,9,'-',decimal];
171		// get length of the value (to loop through)
172		var length = val.length;
173		// loop backwards (to prevent going out of bounds)
174		for(var i = length - 1; i >= 0; i--)
175		{
176			var ch = val.charAt(i);
177			// remove '-' if it is in the wrong place
178			if(i !== 0 && ch == "-")
179			{
180				val = val.substring(0, i) + val.substring(i + 1);
181			}
182			// remove character if it is at the start, a '-' and negatives aren't allowed
183			else if(i === 0 && !negative && ch == "-")
184			{
185				val = val.substring(1);
186			}
187			var validChar = false;
188			// loop through validChars
189			for(var j = 0; j < validChars.length; j++)
190			{
191				// if it is valid, break out the loop
192				if(ch == validChars[j])
193				{
194					validChar = true;
195					break;
196				}
197			}
198			// if not a valid character, or a space, remove
199			if(!validChar || ch == " ")
200			{
201				val = val.substring(0, i) + val.substring(i + 1);
202			}
203		}
204		// remove extra decimal characters
205		var firstDecimal = val.indexOf(decimal);
206		if(firstDecimal > 0)
207		{
208			for(var k = length - 1; k > firstDecimal; k--)
209			{
210				var chch = val.charAt(k);
211				// remove decimal character
212				if(chch == decimal)
213				{
214					val = val.substring(0, k) + val.substring(k + 1);
215				}
216			}
217		}
218		// set the value and prevent the cursor moving to the end
219		this.value = val;
220		$.fn.setSelection(this, carat);
221	}
222};
223
224$.fn.numeric.blur = function()
225{
226	var decimal = $.data(this, "numeric.decimal");
227	var callback = $.data(this, "numeric.callback");
228	var val = this.value;
229	if(val !== "")
230	{
231		var re = new RegExp("^\\d+$|^\\d*" + decimal + "\\d+$");
232		if(!re.exec(val))
233		{
234			callback.apply(this);
235		}
236	}
237};
238
239$.fn.removeNumeric = function()
240{
241	return this.data("numeric.decimal", null).data("numeric.negative", null).data("numeric.callback", null).unbind("keypress", $.fn.numeric.keypress).unbind("blur", $.fn.numeric.blur);
242};
243
244// Based on code from http://javascript.nwbox.com/cursor_position/ (Diego Perini <[email protected]>)
245$.fn.getSelectionStart = function(o)
246{
247	if (o.createTextRange)
248	{
249		var r = document.selection.createRange().duplicate();
250		r.moveEnd('character', o.value.length);
251		if (r.text === '') { return o.value.length; }
252		return o.value.lastIndexOf(r.text);
253	} else { return o.selectionStart; }
254};
255
256// set the selection, o is the object (input), p is the position ([start, end] or just start)
257$.fn.setSelection = function(o, p)
258{
259	// if p is number, start and end are the same
260	if(typeof p == "number") { p = [p, p]; }
261	// only set if p is an array of length 2
262	if(p && p.constructor == Array && p.length == 2)
263	{
264		if (o.createTextRange)
265		{
266			var r = o.createTextRange();
267			r.collapse(true);
268			r.moveStart('character', p[0]);
269			r.moveEnd('character', p[1]);
270			r.select();
271		}
272		else if(o.setSelectionRange)
273		{
274			o.focus();
275			o.setSelectionRange(p[0], p[1]);
276		}
277	}
278};
279
280})(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.