2012-03-27 35 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 

希望幫助