0

我想要验证的移动(iOS 和 Android)应用程序中有多个输入字段。

有些我只喜欢数字,有些不能包含特殊字符,有些需要双打。

有谁知道任何可以处理此问题并管理错误消息的类?

干杯

4

2 回答 2

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");}
于 2012-12-12T08:13:06.183 回答
0

您可以使用 TextInput 类的限制属性。

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

(或者)

           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 = "123@45";
            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)";
}
于 2012-12-12T06:27:09.263 回答