Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the phone number format in Textinput using react-native

Tags:

react-native

i want the phone number(work Phone) format to be as shown in the below image, using react-native,can any one help how to work out it,any help much appreciated enter image description here

like image 447
Hussian Shaik Avatar asked Dec 24 '22 12:12

Hussian Shaik


1 Answers

You can achieve this with regular expressions... This will format like (555) 222-9999

onTextChange(text) {
    var cleaned = ('' + text).replace(/\D/g, '')
    var match = cleaned.match(/^(1|)?(\d{3})(\d{3})(\d{4})$/)
    if (match) {
        var intlCode = (match[1] ? '+1 ' : ''),
            number = [intlCode, '(', match[2], ') ', match[3], '-', match[4]].join('');

        this.setState({
            phoneNum: number
        });

        return;
    }

    this.setState({
        phoneNum: text
    });
}

Then on the <TextInput>...

<TextInput 
    onChangeText={(text) => this.onTextChange(text) }
    value={this.state.phoneNum}
    textContentType='telephoneNumber' 
    dataDetectorTypes='phoneNumber' 
    keyboardType='phone-pad' 
    maxLength={14}
/>
like image 139
Nathan Corbin Avatar answered Dec 31 '22 03:12

Nathan Corbin