我在移动(iOS和Android)应用中有多个输入字段,我想验证。
有些我只喜欢数字,有些人不能包含特殊字符,有些则需要双打。
有没有人知道任何可以处理此问题并管理错误消息的类?
干杯
答案 0 :(得分: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 = "123@45";
Alert.show("Is Contains special char " +regIsSpecialChars.test(special));
您设置的TextInput errorString属性,如
txtUserName.errorString = "It Contains interger";
它将在textinput可视对象周围显示红色边框,并且当鼠标悬停在textinput上时将显示show。
喜欢
var regIsSpecialChars:RegExp = /\W/g;
if(regIsSpecialChars.test(txtUserName.text))
{
txtUserName.errorString = "User Name having some special character(s)";
}
答案 1 :(得分: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");}