2012-03-27 89 views
1

我想要在Adobe Flex中验证IP地址的最佳解决方案。我目前的解决方案是使用正则表达式验证程序并查找虚线的四数字,请参阅下面的代码。出于某种原因,这也将允许像“1.2.3.4.5”这样的地址。有没有人有更好的解决方案?在Adobe Flex中验证IP地址的最佳方法

<?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> 
+0

在http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address看看 – 2012-03-27 19:59:50

回答

0

你可以split("\\.")串,检查所得到的阵列具有4或6的长度,并且每个元素是整数0之间和255

2

正则表达式来验证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 

希望帮助