unity3d 5 InputField 非法路径文件名字符 输入过滤

转载请保留原文链接:http://blog.csdn.net/andyhebear/article/details/51361747

    void Start() {
        if (this.Button == null) {
            this.Button = this.GetComponentInChildren<UI_ButtonClick>();
        }
        if (this.InputText == null) {
            this.InputText = this.GetComponentInChildren<InputField>();
        }
        this.Button.OnClickEvent += Button_OnClickEvent;
        this.InputText.onValidateInput = _OnValidateInput;
        this.InputText.onEndEdit.AddListener(input_OnEndEdit);
    }
    char _OnValidateInput(string text, int charIndex, char addedChar) {       
        if (!_isValidChar(addedChar)) return '\0';//返回空
        return addedChar;
    } 
    //非法字符列表
    private bool _isValidChar(char addedChar) {
        //char[] invalid1 =System.IO.Path.GetInvalidFileNameChars();
        //for (int i = 0; i < invalid1.Length; i++) {
        //    if (invalid1[i] == addedChar) {
        //        return false;
        //    }
        //}
        //char[] invalid2=System.IO.Path.GetInvalidPathChars();
        //for (int i = 0; i < invalid2.Length; i++) {
        //    if (invalid2[i] == addedChar) {
        //        return false;
        //    }
        //}
        for (int i = 0; i < InvalidFilePathNameChars.Length; i++) {
            if (((int)addedChar) == (int)InvalidFilePathNameChars[i]) {
                return false;
            }
        }
        for (int i = 0; i < InvalidCustomChars.Length; i++) {
            if (addedChar == InvalidCustomChars[i]) {
                return false;
            }
        }
        return true;
    }
    /// <summary>
    /// 非法文件名路径 字符char值
    /// GetInvalidFileNameChars(),GetInvalidPathChars()
    /// </summary>
    private static readonly byte[] InvalidFilePathNameChars = new byte[]{
      34,60,62,124,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
      21,22,23,24,25,26,27,28,29,30,31,58,42,63,92,47
    };
    /// <summary>
    /// 自定义非法字符
    /// </summary>
    private static readonly char[] InvalidCustomChars = new char[]{
        '\'','"','~','`','!','@','#','$','%','^','&','*','(',')','+','=','>','<',
        '|','{','}','/','\\',':',';',',','?'        
    };
  
    /// <summary>
    /// 判断字符的Unicode值是否是汉字
    /// </summary>
    /// <param name="code">字符的Unicode</param>
    /// <returns></returns>
    protected static bool IsChineseLetter(int code) {
        int chfrom = System.Convert.ToInt32("4e00", 16);    //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
        int chend = System.Convert.ToInt32("9fff", 16);

        if (code >= chfrom && code <= chend) {
            return true;     //当code在中文范围内返回true

        }
        else {
            return false;    //当code不在中文范围内返回false
        }

        return false;
    }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rains卍Soft

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值