2012-12-12 51 views
0

我想要驗證移動(iOS和Android)應用程序中的多個輸入字段。AS3 AIR移動應用程序輸入驗證

有些我只喜歡數字,其他人不能包含特殊字符,有些需要雙打。

有沒有人知道任何類可以處理這個和管理錯誤消息?

歡呼聲

回答

0

您可以使用TextInput類的restrict屬性。

<s:TextInput id="txtOnlyNumbers" restrict="[0-9]" maxChars="9" /> here no error message will come we are preventing from user typ 

(OR)

  var regOnlyNumbers:RegExp = /[0-9]/g; 
      var numbers:String = "12345"; 
      Alert.show("Is Contains only Numbers " + regOnlyNumbers.test(numbers)); 

      var regIsSpecialChars:RegExp = /\W/g; 
      var special:String = "12345"; 
      Alert.show("Is Contains special char " +regIsSpecialChars.test(special)); 

      var regIsSpecialChars:RegExp = /\W/g; 
      var special:String = "[email protected]"; 
      Alert.show("Is Contains special char " +regIsSpecialChars.test(special)); 

你設定的TextInput errorString屬性像

txtUserName.errorString = "It Contains interger"; 

它會告訴周圍的TextInput可視對象,並顯示紅色邊框將顯示,當鼠標移到了TextInput。

不喜歡

var regIsSpecialChars:RegExp = /\W/g; 
if(regIsSpecialChars.test(txtUserName.text)) 
{ 
    txtUserName.errorString = "User Name having some special character(s)"; 
} 
+0

感謝。我會盡可能檢查它,並讓你知道我如何繼續。 – puks1978

0

另一種方法,請改變您的需求,

package 
{ 
import mx.collections.ArrayCollection; 
import mx.utils.StringUtil; 
import mx.validators.StringValidator; 

public class CommonValidator 
{  

    public function CommonValidator() 
    { 

    } 

    public static function accountNumberValidator(src:Object):Boolean 
    { 
     var regOnlyNumbers:RegExp = /[0-9]/g; 
     var numbers:String = src.text; 

     if(!regOnlyNumbers.test(numbers)) 
     { 
     src.errorString = "It only allow numbers";  
     } 

     return StringUtil.trim(src.errorString).length == 0; 
    } 

    public static function passwordValidator(src:Object):Boolean 
    { 
     var regIsSpecialChars:RegExp = /\W/g; 
     var special:String = src.text; 

     if(regIsSpecialChars.test(special)) 
     { 
      src.errorString = "It contains special chars";    
     } 

     return StringUtil.trim(src.errorString).length == 0; 
    } 

    public static function validateAll_ArrCollection(validationArr:ArrayCollection):Boolean 
    { 
     var arrFailure:Array = []; 
     var isValid:Boolean = false;    

     for(var iIndex:int = 0;iIndex < validationArr.length;iIndex++)  
     { 
      isValid = CommonValidator[validationArr.getItemAt(iIndex).methodName](validationArr.getItemAt(iIndex).srcObject); 

      if(isValid == false) 
       arrFailure.push(isValid); 
     }  

     return arrFailure.length == 0; 

    } 
} 

}

使用

var arrColl:ArrayCollection = new ArrayCollection(); 
    arrColl.addItem({methodName:"accountNumberValidator", srcObject:txtUserName}); 
    arrColl.addItem({methodName:"passwordValidator", srcObject:txtPassword}); 
    if(CommonValidator.validateAll_ArrCollection(arrColl)) 
{ 
trace("All fields are valid"); 
} 
else { 
trace("Fields are invalid");} 
相關問題