'Validate IP Address in React Native

I am looking for a way to validate ip adrress field in my RN mobile app.

But it's taking both valid and invalid ip addresses and also number of digits in each part remains exact three.

Screenshot of App

I am using react-native-mask-input and so far I have tried this:

const ipMask = [
    [/\d/],
    [/\d/],
    [/\d/],
    '.',
    [/\d/],
    [/\d/],
    [/\d/],
    '.',
    [/\d/],
    [/\d/],
    [/\d/],
    '.',
    [/\d/],
    [/\d/],
    [/\d/],
  ];

<View style={{flexDirection: 'row'}}>
        <View style={{flex: 1, flexDirection: 'column'}}>
          <View style={{flexDirection: 'row'}}>
            <Text
              style={{
                fontFamily: 'Titillium-Semibold',
                color: Colors.grey_888888,
                fontSize: 14,
                marginLeft: 13,
              }}>
              IP Address *
            </Text>
          </View>
          <MaskInput
            value={ip}
            mask={ipMask}
            style={{
              marginLeft: 10,
              marginRight: 10,
              marginTop: 5,
              fontSize: 15,
              width: '95%',
              fontFamily: 'Titillium-Semibold',
              fontWeight: 'normal',
              paddingBottom: 0,
              height: 50,
              backgroundColor: '#FAFAFA',
              borderColor: Colors.grey_C0C0C0,
              borderWidth: 1,
              borderRadius: 5,
            }}
            onChangeText={(masked, unmasked, obfuscated) => {
              setIP(masked);
            }}
          />
          <Text
            style={{
              fontFamily: 'Titillium-Semibold',
              color: Colors.red_FF0000,
              fontSize: 11,
              marginLeft: 10,
            }}>
            {requiredMsg.ip && 'IP address is required !!!'}
          </Text>
        </View>
      </View>

Can anyone suggest me a way to validate ip address correctly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source