Apache flex 在AdobeFlex中验证IP地址的最佳方法
我正试图找到在AdobeFlex中验证IP地址的最佳解决方案。我目前的解决方案是使用regexp验证器并查找虚线四元数,请参见下面的代码。出于某种原因,这也将允许类似“1.2.3.4.5”的地址。有谁有更好的解决方案吗Apache flex 在AdobeFlex中验证IP地址的最佳方法,apache-flex,validation,adobe,Apache Flex,Validation,Adobe,我正试图找到在AdobeFlex中验证IP地址的最佳解决方案。我目前的解决方案是使用regexp验证器并查找虚线四元数,请参见下面的代码。出于某种原因,这也将允许类似“1.2.3.4.5”的地址。有谁有更好的解决方案吗 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://n
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:RegExpValidator source="{ipAddr}" property="text"
expression="\d+\.\d+\.\d+\.\d+"
invalid="Alert.show('The IP address is invalid');"/>
</fx:Declarations>
<s:TextInput id="ipAddr" x="10" y="10"/>
<s:Button label="OK" x="10" y="40"/>
</s:Application>
您可以
拆分(“\\”)
字符串,检查结果数组的长度是否为4或6,以及每个元素是否为介于0和255之间的整数,以验证IP是否符合以下要求:
var regexp:RegExp = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
trace(regexp.test("10.0.0.5")); // true
trace(regexp.test("243.1.254.15")); // true
trace(regexp.test("256.0.0.0")); // false
希望有帮助看看