> For the complete documentation index, see [llms.txt](https://docs.verygoodsecurity.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/formatting.md). Agents can also search and read this documentation through its [MCP server](https://docs.verygoodsecurity.com/~gitbook/mcp).

# Formatting

![VGS Collect.js masked input](https://2096104711-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUreALQAfVnRMQEz110rC%2Fuploads%2Fgit-blob-a0f3b2f7dd50ffeda4a60fc57344124e46d04d08%2Fmasking-collect.gif?alt=media)

## Serializers

Use serializers to change the format of the data being sent to the server.

*Examples:*

{% code title="Replace value" %}

```javascript
form.field('#space-for-my-field', {
    serializers: [{ name: 'replace', options: { old:' ', new:'-' }}],
    serializers: [{ name: 'replace', options: { old:' ', new:'-', count: '2' }}]
});
```

{% endcode %}

{% code title="Expiration date separator" %}

```javascript
form.field('#space-for-my-field', {
    serializers: [{ name: 'separate', options: { monthName: 'month', yearName: 'year' }}]
});
```

{% endcode %}

{% code title="Convert to base64" %}

```javascript
form.field('#space-for-my-field', {
    type: 'file'
    serializers: [{ name: 'toBase64'}]
});
```

{% endcode %}

* [.field()](https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/reference-documentation.md#api-formfield)
* [Reference Documentation](https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/reference-documentation.md)

## Custom masking

An input mask refers to a string expression, defined by a developer, that constrains user input. This method is available for those types of fields: `text`, `textarea`, `password`, `zip-code`.

Example:

```javascript
f.field('#ssn .field-space', {}).mask('999-99-9999', '*');

f.field('#random-mask .field-space', {}).mask('XXXXX', null, {'X': '[0-9]'});
```

* [.mask()](https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/reference-documentation.md#api-formcardcvcfield-16)
* [Reference Documentation](https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/reference-documentation.md)

## Define string replace pattern

Method returns a new string with some or all matches of a pattern replaced by a replacement. This method is available for those types of fields: `text`, `textarea`, `password`, `zip-code`.

Example:

```javascript
f.field('#letters-only .field-space', {}).replacePattern('/[^a-zA-Z\s]+/g');

f.field('#numeric-only .field-space', {}).replacePattern('/[^\d]+/g');
```

* [.mask()](https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/reference-documentation.md#api-formcardcvcfield-16)
* [Reference Documentation](https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/reference-documentation.md)

## More examples

* [Control expiration date year length](https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/samples.md#control-expiration-date-year-length)
* [Separate month and year for the card expiration date field](https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/samples.md#separate-month-and-year-for-the-card-expiration-date-field)
* [Change expiration date separator](https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/samples.md#change-expiration-date-separator)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.verygoodsecurity.com/vault/developer-tools/vgs-collect/js/formatting.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
