Código de JavaScript - formatear un numero según es introducido en un input de un formulario

Imágen de perfil
Val: 61
Ha disminuido 1 puesto en JavaScript (en relación al último mes)
Gráfica de JavaScript

formatear un numero según es introducido en un input de un formulariográfica de visualizaciones


JavaScript

Publicado el 28 de Enero del 2021 por Aitor (6 códigos)
178 visualizaciones desde el 28 de Enero del 2021
Rutina en JavaScript para formatear el campo input de un formulario según se va introduciendo con el teclado.

Hay varias clases:
Números enteros positivos y negativos o solo los positivos.
Números flotantes positivos y negativos o solo los positivos.
Fijación de números flotantes a 2 decimales.

Se puede indicar el formato de los separadores de miles y de decimales entre el formato USA que usa como separador de miles la coma "," y el separador de decimales el punto "." o viceversa como en España.

Demo y copia gratuita de:
https://www.bc3toexcel.com/compartir/input-format-number-as-keyed.htm

En el navegador pulsando botón derecho del ratón opción mostrar código fuente se puede copiar.
format-number-as-keyed

1.0

Publicado el 28 de Enero del 2021gráfica de visualizaciones de la versión: 1.0
179 visualizaciones desde el 28 de Enero del 2021
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>FORMAT (PUT THOUSANDS SEPARATOR) INSIDE A FORM NUMERIC INPUT FIELD AS THE VALUE IS KEYED</title>
        <style>
            input {padding: 20px;margin: 10px;font-size: 18px;}
            .label,
            .integerPositive,
            .integerIndistinct,
            .numberPositive,
            .numberIndistinct,
            .numberPositiveFixed,
            .numberIndistinctFixed {text-align: right;}
            .positives {color: black;}
            .negatives {color: red;}
            .oculto{display:none;}
            .visible{display:block;}
            .table_data_received table,
            .table_data_received tr,
            .table_data_received th,
            .table_data_received td{border: 1px solid black;text-align:right;}
        </style>
    </head>
    <body>
        <h2>FORMAT (PUT THOUSANDS SEPARATOR) INSIDE A FORM NUMERIC INPUT FIELD AS THE VALUE IS KEYED</h2>
        <!-- ONLY FOR TESTING PURPOSE -->
        <div id="data_received" class="oculto">
            <h2>DATA RECEIVED FROM FORM</h2>
        </div>
        <!-- TEST FORM SAMPLE-->
        <form id="TheForm" class="oculto" method="get" action="input-format-number-as-keyed.htm" onsubmit="return removeNumberFormat(true);">
            <table>
                <tr>
                    <td class="label">Integer(+/-)</td>
                    <td><input class="integerIndistinct" name="integer1" id="integer1"></td>
                </tr>
                <tr>
                    <td class="label">Integer(+)&gt;= 0</td>
                    <td><input class="integerPositive" name="integer2" id="integer2"></td>
                </tr>
                <tr>
                    <td class="label">Float(+/-)</td>
                    <td><input class="numberIndistinct" name="number1" id="number1"></td>
                </tr>
                <tr>
                    <td class="label">Float(+)&gt;= 0</td>
                    <td><input class="numberPositive" name="number2" id="number2"></td>
                </tr>
                <tr>
                    <td class="label">Float Fixed 2 (+/-)</td>
                    <td><input class="numberIndistinctFixed" name="number3" id="number3"></td>
                </tr>
                <tr>
                    <td class="label">Float Fixed 2 (+)&gt;= 0</td>
                    <td><input class="numberPositiveFixed" name="number4" id="number4"></td>
                </tr>
            </table>
            <input type="submit" value="submit">
        </form>
        <script>
        //================================      ======================================
        //==============   THIS PART OF THE CODE IS ONLY FOR TESTING    ==============
        //================================      ======================================
        //To test if the form submitted data has been changed removing the thousands
        //separators from the numeric values and show the result in dev.tools console.
        //Can be eliminated after test is successful
        //============================================================================
        function reset_form() {
        	window.location.replace(location.pathname);
        }
        const getURLParameters = url =>
        (
        /*
         * extract the parameters from the url that has been called
         * @param  {string} the url that has been called
         * @return {array} returns an associative array of the input variables
         */
        	url.match(/([^?=&]+)(=([^&]*))/g) ||[]).reduce(
        	(a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a), {}
        );
        let array_param = getURLParameters(window.location.href);
        let array_keys = Object.keys(array_param);
        if (array_keys.length!==0) {
        	let div = document.getElementById("data_received");
        	let table = document.createElement("table");
        	table.className = "table_data_received";
        	let tr = document.createElement("tr");
        	let len = array_keys.length;
        	for(let i = 0; i < len; i++) {
        		let th = document.createElement("th");
        		th.innerHTML = decodeURIComponent(array_keys[i]);
        		tr.appendChild(th);
        	}
        	table.appendChild(tr);
        	tr = document.createElement("tr");
        	for(let j = 0; j < len; j++) {
        		let td = document.createElement("td");
        		td.innerHTML = decodeURIComponent(array_param[array_keys[j]]);
        		tr.appendChild(td);
        	}
        	table.appendChild(tr);
        	div.appendChild(table);
        	let button = document.createElement("input");
        	button.setAttribute("style", "cursor:pointer; margin-top:10px;");
        	button.setAttribute("type", "button");
        	button.setAttribute("value", "Repeat");
        	button.setAttribute("onclick", "reset_form();");
        	div.appendChild(button);
        	div.className = "visible";
        	let form = document.getElementById("TheForm");
        	form.className = "oculto";
        }else {
        	let form = document.getElementById("TheForm");
        	form.className = "visible";
        }
        //================================      ======================================
        //==========================   END OF TESTING    =============================
        //================================      ======================================
        //############################################################################
        /**
         * @format-numeric-input-as-keyed.js
         * @version     : 1.0
         * @author      : Aitor Solozabal Merino (aitorsolozabal@gmail.com)
         * @purpose     : Format a number (with thousands separator) in an input field
         *                of a form as it is being entered key by key on the keyboard
         * History
         * @version     : v1.0 – initial
         * FUNCTIONS LIST
         * ==============
         * function integerFormatIndistinct()
         * function integerFormatPositive()
         * function numberFormatIndistinct()
         * function numberFormatPositive()
         * function numberFormatIndistinctFixed()
         * function numberFormatPositiveFixed()
         * There are classes / categories defined to associate with each function and that
         * are related when declaring the keyup event for each one of them
         *   .integerPositive,
         *   .integerIndistinct,
         *   .numberPositive,
         *   .numberIndistinct,
         *   .numberPositiveFixed,
         *   .numberIndistinctFixed {text-align: right;}
         *   .positives {color: black;}
         *   .negatives {color: red;}
         * function numberFormat(strNumber, n, thousands, decimals, minusSigned = true)
         * function removeNumberFormat(emptyValues = true) to be call from form submit
         */
        //############################################################################
        // global variables to define the thousands and decimals separator
        // For thousands a comma and a point for decimal (USA)
        let DECIMALS = ".";
        let THOUSANDS = ",";
        // change to "," for decimal comma and point in thousands (SPAIN)
        // =============================================== ===========================
        function numberFormat(strNumber, n, thousands, decimals, minusSigned = true) {
        /**
         *This function is called from other functions that are activated when the "keyup"
         *event occurs when typing from the keyboard by entering a character within a text
         *entry in a form that you want to give a numeric format.
         *
         *As it is allowed to add characters to the beginning in the middle or at the
         *end of the input text of the form, cases of generation of invalid numbers
         *can occur due to the concurrence of multiple factors.
         *Cases to control of strNumber content with the rule that there can only be
         *one minus symbol and it must be at the beginning and a single decimal
         *separator that can be anywhere.
         *In addition, the following combinations must be processed
         * "-" "." "-." "--" ".." "0" "-0" "-0." "0." "00" "0n" ".n" ".n." "-n-"
         *
         * This function also serves to return a formatted number for printing or display
         * on the screen independently of using it in the numeric fields of a form
         * ===============================================================================
         * @param string strNumber: string with the number to format
         * @param integer n: length of decimal (>0)fixed (=0)integer (=-1)float not fixed
         * @param string  thousands: sections delimiter THOUSANDS  USA "," SPAIN "."
         * @param string  decimals: decimal separator   DECIMALS   USA "." SPAIN ","
         * ===============================================================================
         * @VERSION  : 1.0
         * @DATE     : 27 January 2020
         * @AUTHOR   : Aitor Solozabal Merino (aitorsolozabal@gmail.com)
         * sample use:
         * integer not minus
         * console.log("integer "+"-123456.789"+"  0 decimales = "+numberFormat("-123456.789", 0, ",", ".",false));
         * output 123,456
         * float indistinct not fixed
         * console.log("float "+"-123456.789"+"  -1 decimales = "+numberFormat("-123456.789", -1, ",", ".",true));
         * output -123,456.789
         * float fixed not minus
         * console.log("float "+"-123456.789"+"   2 decimales = "+numberFormat("-123456.789",  2, THOUSANDS, DECIMALS,false));
         * output 123,456.78
         */
        	let re = '\\d(?=(\\d{3})+$)';
        	let resultado = "";
        	let hasminusSymbol = false;
        	let hasdecimalsSeparator = false;
        	let nextCharacter = "";
        	let initialZero = false;
        	let numParts =[];
        	let integerPart = "";
        	let decimalPart = "";
        	let firstDecimal = -1;
        	//filter (minusSigned && n!==0) numbers, minus symbol, decimals separator
        	let filterNumber1 = new RegExp('[^ 0-9\\-\\'+decimals+']', 'g');
        	//filter (minusSigned && n===0) numbers, minus symbol not decimals separator
        	let filterNumber2 = new RegExp('[^ 0-9\\-]', 'g');
        	//filter (!minusSigned && n!==0) numbers, decimals separator not minus symbol
        	let filterNumber3 = new RegExp('[^ 0-9\\'+decimals+']', 'g');
        	//filter (!minusSigned && n===0) only numbers are permitted
        	let filterNumber4 = new RegExp('[^ 0-9]', 'g');
        	//Only numbers, decimals separator and minus symbol are allowed, so the
        	//number is filtered to eliminate those characters that are not valid
        	//================================================================== F I R S T
        	//first filtering of invalid characters
            //the thousands delimeters are eliminated in this filter process
        	strNumber = strNumber.replace(filterNumber1, "");
        	if (strNumber.length>0) {
        		//============================================================ S E C O N D
        		//If the minusSigned parameter is false, then this symbol is not allowed
        		//and only positive values are valid.
        		//Therefore you have to see if the first character is the minus symbol "-"
        		//and, if so, it must be removed to be added to the final result of the
        		//formatting process if the minusSigned parameter is true.
        		hasminusSymbol = (strNumber.substring(0, 1)==="-");
        		strNumber = strNumber.replace(filterNumber3, "");
        		if (strNumber.length>0) {
        			//========================================================== T H I R D
        			//if the first character is the decimal separator strNumber must be
        			//filtered eliminating all ocurrences of the decimalseparator in
        			//strNumber with a filter and then it is added with the zero character
        			//previously "." --> "0."
        			hasdecimalsSeparator = (strNumber.substring(0, 1)===decimals);
        			strNumber = (hasdecimalsSeparator)? "0.": strNumber;
        			if (!hasdecimalsSeparator) {
        				//if there is no decimal separator at the beginning, check if there
        				//is a decimal separator and check that this is the only one by
        				//erasing the excess ones
        				firstDecimal = strNumber.indexOf(decimals);
                        hasdecimalsSeparator = (firstDecimal>0);
    					//There is at least one decimal separator in strNumber
    					//Clean any other occurrence of a decimal separator in strNumber
    					strNumber = hasdecimalsSeparator?strNumber.substring(0, firstDecimal)+decimals+strNumber.substring(firstDecimal+1, strNumber.length).replace(filterNumber4, ""):strNumber;
        			}
        			//======================================================== F O U R T H
        			//"0"
        			initialZero = (strNumber.substring(0, 1)==="0");
        			//the first character can be a 0 or a decimal separator
        			if (initialZero) {
        				//if the first character is the number zero then the next character
        				//must be obtained and find out if it is a number or the decimal
        				//separator.
        				//It could be a "0." or a "0n"
        				nextCharacter = (strNumber.length>1)? strNumber.substring(1, 2): "";
    					//strNumber is of type "0n"
    					//"01"     "1" / "00"    "0"
    					//In this case the leading zero character is removed
    				    strNumber = ((nextCharacter.length>0) && (nextCharacter!==decimals))?strNumber.substring(1, strNumber.length):strNumber;
        			}
        			//========================================================== F I F T H
        			//strNumber is divided into 2 parts using the defined decimal separator
        			//character as an element that separates the integer part and the
        			//decimal part if it exists
        			numParts = strNumber.split(decimals);
        			integerPart = numParts[0];
        			decimalPart = (numParts.length>1)? numParts[1]: "";
        			//========================================================== S I X T H
        			// add the thousands separators
        			resultado = integerPart.replace(new RegExp(re, 'g'), '$&' + (thousands));
        			//====================================================== S E V E N T H
        			//join the 2 parts to obtain the result with the established format
        			if (n!==0) {
        				if (n>0) {
        					//fixed decimals
        					decimalPart = (decimalPart.length>n)? decimalPart.substring(0, n): decimalPart;
        				}
    					resultado = (hasdecimalsSeparator)? resultado+decimals+decimalPart: resultado;
        			}
        		}
        	}
        	//================================================================ E I G H T H
        	//check if the minus symbol is allowed so that if it exists add it to the final
        	//result before being returned
        	resultado = (minusSigned && hasminusSymbol)? "-"+resultado: resultado;
        	return resultado;
        }
        function integerFormatIndistinct() {
        	// Format integers indistinctly both positive and negative
        	if (this.value.length>0) {
        		this.value = numberFormat(this.value, 0, THOUSANDS, DECIMALS, true);
        		this.className = (this.value.substring(0, 1)==="-")? "negatives integerIndistinct": "positives integerIndistinct";
        	}
        }
        function integerFormatPositive() {
        	// Format only positive integers
        	this.value = this.value.length>0? numberFormat(this.value, 0, THOUSANDS, DECIMALS, false): "";
        }
        function numberFormatIndistinct() {
        	// Format decimal numbers indistinctly both positive and negative
        	if (this.value.length>0) {
                this.value = numberFormat(this.value, -1, THOUSANDS, DECIMALS, true);
        		this.className = (this.value.substring(0, 1)==="-")? "negatives integerIndistinct": "positives integerIndistinct";
        	}
        }
        function numberFormatPositive() {
        	// Format only positive decimal numbers
        	this.value = this.value.length>0? numberFormat(this.value, -1, THOUSANDS, DECIMALS, false): "";
        }
        function numberFormatIndistinctFixed() {
        	// Format decimal numbers indistinctly both positive and negative
        	// but with 2 decimal places fixed as maximum
        	if (this.value.length>0) {
            	this.value = numberFormat(this.value, 2, THOUSANDS, DECIMALS, true);
        		this.className = (this.value.substring(0, 1)==="-")? "negatives integerIndistinct": "positives integerIndistinct";
        	}
        }
        function numberFormatPositiveFixed() {
        	// Format only positive decimal numbers with 2 decimal places fixed as maximum
        	this.value = this.value.length>0? numberFormat(this.value, 2, THOUSANDS, DECIMALS, false): "";
        }
        function removeNumberFormat(emptyValues = true) {
        	//************************************************************************
        	//The text input fields that contain the formatted numbers of the form must be
        	//prepared before they are sent, eliminating the thousands separators and
        	//placing the decimal separators of the system to transmit the data.
        	//If the boolean variable emptyValues is true, then all values will be sent
        	//including those that are empty, otherwise only non-empty values will be sent
        	//This function is called with the instruction in the <form> tag
        	//onsubmit = "return removeNumberFormat(emptyValues);"
        	//************************************************************************
        	let filterNumber = new RegExp('[^ 0-9\\-' + '\\' + DECIMALS + ']', 'g');
        	let integer1 = document.getElementById('integer1');
        	//eliminate thousands separator with the filterNumber
        	integer1.value = integer1.value.replace(filterNumber, "");
        	//to remove the field integer1 from submit data if it is empty then:
        	if (!emptyValues) {
        		if (integer1.value==="") {integer1.parentNode.removeChild(integer1);}
        	}
        	let integer2 = document.getElementById('integer2');
        	//eliminate thousands separator with the filterNumber
        	integer2.value = integer2.value.replace(filterNumber, "");
        	//to remove the field integer2 from submit data if it is empty then:
        	if (!emptyValues) {
        		if (integer2.value==="") {integer2.parentNode.removeChild(integer2);}
        	}
        	let number1 = document.getElementById('number1');
        	//eliminate thousands separator with the filterNumber
        	number1.value = number1.value.replace(filterNumber, "");
        	number1.value = number1.value.replace(DECIMALS, ".");
        	//to remove the field number1 from submit data if it is empty then:
        	if (!emptyValues) {
        		if(number1.value==="") {number1.parentNode.removeChild(number1);}
        	}
        	let number2 = document.getElementById('number2');
        	//eliminate thousands separator with the filterNumber
        	number2.value = number2.value.replace(filterNumber, "");
        	//replace decimals separator in format number with system decimal separator
        	number2.value = number2.value.replace(DECIMALS, ".");
        	//to remove the field number2 from submit data if it is empty then:
        	if (!emptyValues) {
        		if(number2.value==="") {number2.parentNode.removeChild(number2);}
        	}
        	let number3 = document.getElementById('number3');
        	//eliminate thousands separator with the filterNumber
        	number3.value = number3.value.replace(filterNumber, "");
        	//replace decimals separator in format number with system decimal separator
        	number3.value = number3.value.replace(DECIMALS, ".");
        	//to remove the field number3 from submit data if it is empty then:
        	if (!emptyValues) {
        		if(number3.value==="") {number3.parentNode.removeChild(number3);}
        	}
        	let number4 = document.getElementById('number4');
        	//eliminate thousands separator with the filterNumber
        	number4.value = number4.value.replace(filterNumber, "");
        	//replace decimals separator in format number with system decimal separator
        	number4.value.replace(DECIMALS, ".");
        	//to remove the field number4 from submit data if it is empty then:
        	if (!emptyValues) {
        		if(number4.value==="") {number4.parentNode.removeChild(number4);}
        	}
        	return true;
        }
        //////////////////////////////////////////////////////////////////////////////////
        window.onload = function() {
        	// IT IS EXECUTED AFTER LOADING ALL THE CSS & HTML & JAVASCRIPT CODE
        	//============================================================================
        	// Create the keyup event for each defined class and relate it to the appropriate function
        	//============================================================================
        	document.querySelectorAll(".integerIndistinct").forEach(el => el.addEventListener("keyup", integerFormatIndistinct));
        	document.querySelectorAll(".integerPositive").forEach(el => el.addEventListener("keyup", integerFormatPositive));
        	document.querySelectorAll(".numberIndistinct").forEach(el => el.addEventListener("keyup", numberFormatIndistinct));
        	document.querySelectorAll(".numberPositive").forEach(el => el.addEventListener("keyup", numberFormatPositive));
        	document.querySelectorAll(".numberIndistinctFixed").forEach(el => el.addEventListener("keyup", numberFormatIndistinctFixed));
        	document.querySelectorAll(".numberPositiveFixed").forEach(el => el.addEventListener("keyup", numberFormatPositiveFixed));
        };
        //////////////////////////////////////////////////////////////////////////////////
        </script>
    </body>
</html>



Comentarios sobre la versión: 1.0 (0)


No hay comentarios
 

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6850