Estoy buscando una forma de validar el campo de dirección IP (onChange) en mi aplicación móvil RN.
Pero está tomando direcciones IP válidas e inválidas y también el número de dígitos en cada parte sigue siendo exactamente tres.
Estoy usando react-native-mask-input
y hasta ahora he intentado esto:
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>
¿Alguien puede sugerirme una forma de validar la dirección IP correctamente?
Obtenga el paquete is-ip
de NPM. El enlace aquí puede ser útil.
Luego, simplemente puede usar las funciones para verificar. P.ej:
isIP('192.168.0.1'); // returns true
Encontrará más ejemplos de código en la página web anterior.
EDITAR: Acabo de notar la fecha de la pregunta... :')