簡體   English   中英

在 React Native 中驗證 IP 地址

[英]Validate IP Address in React Native

我正在尋找一種方法來驗證我的 RN 移動應用程序中的 ip 地址字段 (onChange)。

但它同時采用了有效和無效的 ip 地址,並且每個部分中的位數仍然是精確的三位。

Screenshot of App

我正在使用react-native-mask-input ,到目前為止我已經嘗試過:

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>

誰能建議我正確驗證 ip 地址的方法?

從 NPM 獲取is-ip package。 此處的鏈接可能會有所幫助。

然后,您可以簡單地使用這些功能進行檢查。 例如:

isIP('192.168.0.1'); // returns true

您可以在上面的網頁中找到更多代碼示例。

編輯:我剛剛注意到問題的日期...... :')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM