提问人:Sami 提问时间:2/2/2022 更新时间:2/2/2022 访问量:188
Angularjs:Mac 地址验证不起作用
Angularjs: Validation for Mac address not working
问:
我有一个表单来添加设备 mac 地址和 wifi mac 地址,但我喜欢对输入表单进行验证,只是为了接受此模式“xx:xx:xx:xx:xx:xx”上的字母数字值。
我最初对 IP 地址进行了验证,它工作正常,如下所示:
<label class="input">
<input type="text"
name="textreadOnlyInfoWiFiIPAddress"
ng-model="readOnlyInfo.WiFiIPAddress"
ng-value="readOnlyInfo.WiFiIPAddress"
ng-pattern='/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/'
ng-model-options="{ updateOn: 'blur' }"
placeholder='xxx.xxx.xxx.xxx'>
</label>
现在我正在尝试为 Mac 地址实现相同的方法:
<input type="text"
name="textreadOnlyInfoWifiMACAddress"
ng-model="readOnlyInfo.WifiMACAddress"
ng-value="readOnlyInfo.WifiMACAddress"
ng-pattern='/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/'
ng-model-options="{ updateOn: 'blur' }"
placeholder='xx:xx:xx:xx:xx:xx'>
</label>
并且它没有按预期工作,在这种情况下,mac 地址的 ng-pattern 如何?
答:
0赞
Sami
2/2/2022
#1
我想到了下面的答案:
<input type="text"
name="textreadOnlyInfoMACAddress"
ng-model="readOnlyInfo.MACAddress"
ng-value="readOnlyInfo.MACAddress"
ng-pattern="/[A-F0-9]{2}\:[A-F0-9]{2}\:[A-F0-9]{2}\:[A-F0-9]{2}\:[A-F0-9]{2}\:[A-F0-9]{2}/"
ng-model-options="{ updateOn: 'blur' }"
placeholder='xx:xx:xx:xx:xx:xx'>
</label>
以下模式确保接受大写字母和数值(字母数字格式)的 MAC 地址。
ng-pattern="/[A-F0-9]{2}\:[A-F0-9]{2}\:[A-F0-9]{2}\:[A-F0-9]{2}\:[A-F0-9]
MAC地址的示例:“00:1A:7B:0F:4E:4N”,需要上述模式。
但是,当MAC地址包含小写字母时,例如“r0:h1:nc:23:5a:53”,则需要以下模式:
ng-pattern="/[a-f0-9]{2}\:[a-f0-9]{2}\:[a-f0-9]{2}\:[a-f0-9]{2}\:[a-f0-9]{2}\:[a-f0-9]{2}/"
评论