Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Un campo de entrada de texto almacena una cadena como su valor y una cadena como su texto. Su valor siempre es una cadena válida, mientras que su texto puede ser cualquier cadena ingresada en su editor.
El constructor de entrada de texto toma un valor opcional y un validador opcional. El valor se debe transmitir a una cadena. Si es null o undefined, se usará una cadena vacía.
La definición JSON también te permite establecer la opción spellcheck.
Serialización y XML
JSON
El JSON de un campo de entrada de texto se ve de la siguiente manera:
{"fields":{"FIELDNAME":"text"}}
En la que FIELDNAME es una cadena que hace referencia a un campo de entrada de texto y el valor es el valor que se aplicará al campo. El valor sigue las mismas reglas que el valor del constructor.
XML
El XML de un campo de entrada de texto se ve de la siguiente manera:
<fieldname="FIELDNAME">text</field>
En el que el atributo name del campo contiene una cadena que hace referencia a un campo de entrada de texto y el texto interno es el valor que se aplicará al campo. El valor de texto interno sigue las mismas reglas que el valor del constructor.
Personalización
Corrector ortográfico
La función setSpellcheck se puede usar para establecer si el campo corrige la ortografía del texto de entrada o no.
Campos de entrada de texto con y sin corrector ortográfico
La corrección ortográfica está activada de forma predeterminada.
Esto se aplica a campos individuales. Si deseas modificar todos los campos, cambia la
propiedad Blockly.FieldTextInput.prototype.spellcheck_.
Cómo crear un validador de entrada de texto
El valor de un campo de entrada de texto es una cadena, por lo que cualquier validador debe aceptar una cadena y mostrar una cadena, null o undefined.
Este es un ejemplo de un validador que quita todos los caracteres "a" de la cadena:
[null,null,["Última actualización: 2025-07-25 (UTC)"],[[["\u003cp\u003eA text input field stores a string value and allows user text input, with the value always being a valid string.\u003c/p\u003e\n"],["\u003cp\u003eYou can create text input fields using JSON or JavaScript, customizing them with options like spellcheck and validators.\u003c/p\u003e\n"],["\u003cp\u003eText input fields can be serialized and deserialized using JSON or XML, representing the field name and value.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003esetSpellcheck\u003c/code\u003e function allows control over individual field spellchecking, while \u003ccode\u003eBlockly.FieldTextInput.prototype.spellcheck_\u003c/code\u003e affects all fields.\u003c/p\u003e\n"],["\u003cp\u003eValidators for text input fields accept a string and return a modified string, null, or undefined to enforce specific input rules.\u003c/p\u003e\n"]]],["Text input fields store a string as both their value and text, with the value always being a valid string. Creation involves defining the field in JSON or JavaScript, specifying a default text and optional spellcheck. The constructor and JSON allow setting a value, defaulting to an empty string if `null` or `undefined`. Serialization uses JSON and XML, where field names and values are stored. Spellcheck can be toggled, and validators are functions that accept and return strings, `null`, or `undefined`.\n"],null,["# Text input fields\n\nA text input field stores a string as its value and a string as its text. Its\nvalue is always a valid string, while its text could be any string entered into\nits editor.\n\n#### Text input field\n\n#### Text input field with editor open\n\n#### Text input field on collapsed block\n\nCreation\n--------\n\n### JSON\n\n {\n \"type\": \"example_textinput\",\n \"message0\": \"text input: %1\",\n \"args0\": [\n {\n \"type\": \"field_input\",\n \"name\": \"FIELDNAME\",\n \"text\": \"default text\",\n \"spellcheck\": false\n }\n ]\n }\n\n### JavaScript\n\n Blockly.Blocks['example_textinput'] = {\n init: function() {\n this.appendDummyInput()\n .appendField(\"text input:\")\n .appendField(new Blockly.FieldTextInput('default text'),\n 'FIELDNAME');\n }\n };\n\nThe text input constructor takes in an optional value and an optional\n[validator](#creating_a_text_input_validator). The value should cast to a\nstring. If it is `null` or `undefined`, an empty string will be used.\n\nThe JSON definition also allows you to set the [spellcheck](#spellcheck) option.\n\nSerialization and XML\n---------------------\n\n### JSON\n\nThe JSON for a text input field looks like so: \n\n {\n \"fields\": {\n \"FIELDNAME\": \"text\"\n }\n }\n\nWhere `FIELDNAME` is a string referencing a text input field, and\nthe value is the value to apply to the field. The value\nfollows the same rules as the constructor value.\n\n### XML\n\nThe XML for a text input field looks like so: \n\n \u003cfield name=\"FIELDNAME\"\u003etext\u003c/field\u003e\n\nWhere the field's `name` attribute contains a string referencing a text input\nfield, and the inner text is the value to apply to the field. The inner\ntext value follows the same rules as the constructor value.\n\nCustomization\n-------------\n\n### Spellcheck\n\nThe\n[setSpellcheck](/blockly/reference/js/Blockly.FieldTextInput#setSpellcheck)\nfunction can be used to set whether the field spellchecks its input text or not.\n\n### Text input fields with and without spellcheck\n\nSpellchecking is on by default.\n\nThis applies to individual fields. If you want to modify all fields change the\n`Blockly.FieldTextInput.prototype.spellcheck_` property.\n\nCreating a text input validator\n-------------------------------\n\n| **Note:** For information on validators in general see [Validators](/blockly/guides/create-custom-blocks/fields/validators).\n\nA text input field's value is a string, so any validators must accept a string\nand return a string, `null`, or `undefined`.\n\nHere is an example of a validator that removes all 'a' characters from the\nstring: \n\n function(newValue) {\n return newValue.replace(/a/g, '');\n }"]]