Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
react-native-masked-text
Advanced tools
This is a simple masked text (normal text and input text) component for React-Native.
Thanks to vanilla-masker =). Thanks to moment =).
React-native: 0.32.0 or higher
npm install react-native-masked-text --save
import React, {Component} from 'react';
// import the component
import {TextInputMask} from 'react-native-masked-text';
export default class MyComponent extends Component {
constructor(props) {
super(props);
}
isValid() {
// isValid method returns if the inputed value is valid.
// Ex: if you input 40/02/1990 30:20:20, it will return false
// because in this case, the day and the hour is invalid.
let valid = this.refs['myDateText'].isValid();
// get converted value. Using type=datetime, it returns the moment object.
// If it's using type=money, it returns a Number object.
let rawValue = this.refs['myDateText'].getRawValue();
}
render() {
// the type is required but options is required only for some specific types.
return (
<TextInputMask
ref={'myDateText'}
type={'datetime'}
options={{
format: 'DD-MM-YYYY HH:mm:ss'
}} />
);
}
}
type
credit-card: use the mask 9999 9999 9999 9999. It accepts options (see later in this doc).
cpf: use the mask 999.999.999-99
and numeric
keyboard.
cnpj: use the mask 99.999.999/9999-99
and numeric
keyboard.
zip-code: use the mask 99999-999
and numeric
keyboard.
only-numbers: accept only numbers on field with numeric
keyboard.
money: use the mask R$ 0,00
on the field with numeric
keyboard. It accepts options (see later in this doc).
cel-phone: use the mask (99) 9999-9999
or (99) 99999-9999
(changing automaticaly by length). It accepts options (see later in this doc).
datetime: use datetime mask with moment format (default DD/MM/YYYY HH:mm:ss). It accepts options (see later in this doc).
custom: use your custom mask (see the options props later in this doc).
TextInput Props
You can use the native props of TextInput, with this in mind:
Options
Some types accept options, use it like this: <TextInputMask type={'money'} options={{ unit: 'US$' }} />
For type={'money'}
precision
(Number, default 2): the decimal places.separator
(String, default ','): the decimal separator.delimiter
(String, default '.'): the thousand separator.unit
: (String, default 'R$'): the prefix text.suffixUnit
(String, default ''): the suffix text.zeroCents
(Boolean, default false): if must show cents.For type={'cel-phone'}
withDDD
(Boolean, default true): if the ddd will be include in the mask.dddMask
(String, default '(99) '): the default mask applied if withDDD
is true.For type={'datetime'}
format
(String, default DD/MM/YYYY HH:mm:ss): moment date format. It accepts the following:
-
instead of /
if you wantFor type={'custom'}
mask
(String, default ''): your mask template.
9
: accept digit.A
: accept alpha.S
: accept alphanumeric.validator
(Function, default returns true): the function use to validate value. Must return true if valid or false if invalid.
value
: current value.settings
: current settingsgetRawValue
(Function, default returns the current value): the function that's invoked when getRawValue
method from component is called.
value
: current value.settings
: current settingsFor type={'credit-card'}
obfuscated
(Boolean, default false): if the mask must be 9999 **** **** 9999
getElement()
: return the instance of TextInput component.isValid()
: if the value inputed is valid for the mask.
getRawValue()
: get the converted value of mask.
1234 1234 1234 1234
returns [1234, 1234, 1234, 1234]
.R$ 1.234,56
returns 1234.56
.moment
object for the date and format.import React, {Component} from 'react';
// import the component
import {TextMask} from 'react-native-masked-text';
export default class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
text: '4567123409871234'
};
}
render() {
// the type is required but options is required only for some specific types.
// the sample below will output 4567 **** **** 1234
return (
<TextMask
value={this.state.text}
type={'credit-card'}
options={{
obfuscated: true
}} />
);
}
}
The same of TextInputMask, but for React-Native Text component instead TextInput.
Warning: if the value not match the mask, it will not appear.
getElement()
: return the instance of Text component.
If you want, we expose the MaskService
. You can use it:
Methods
type
(String, required): the type of the mask (cpf
, datetime
, etc...)value
(String, required): the value to be maskedsettings
(Object, optional): if the mask type accepts options, pass it in the settings parametertype
(String, required): the type of the mask (cpf
, datetime
, etc...)value
(String, required): the value to be maskedsettings
(Object, optional): if the mask type accepts options, pass it in the settings parameterEx:
import {MaskService} from 'react-native-masked-text'
var money = MaskService.toMask('money', '123', {
unit: 'US$',
separator: '.',
delimiter: ','
});
// money -> US$ 1.23
getRawValue
.1.2.1
FAQs
Text and TextInput with mask for React Native applications
The npm package react-native-masked-text receives a total of 29,493 weekly downloads. As such, react-native-masked-text popularity was classified as popular.
We found that react-native-masked-text demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.