一、只允许输入汉字、大小写字母、数字、减号二、只允许输入大小写字母、数字、减号

解决方案 »

  1.   

    ^.[A-Za-z0-9]+$  在网上找的 如果 输入 %786sdf IsMatch也返回TRUE
      

  2.   

    一、^[a-zA-Z0-9\-\u4E00-\u9FA5\uF900-\uFA2D]+$
    二、^[a-zA-Z0-9\-]+$
      

  3.   

    \uF900-\uFA2D
    这是什么的范围阿??
      

  4.   

    只要找到是否有 非数字,非字母,非减号,非中文的字符存在就可以了。取反一下输出,我在C# winform测试using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;namespace test.CSDN.ChineseOnly
    {
        public partial class ChineseOnly : Form
        {
            public ChineseOnly()
            {
                InitializeComponent();
            }        private void button_test_Click(object sender ,EventArgs e)
            {
                bool result = CheckInput(this.textBox1.Text.Trim());
                MessageBox.Show(result.ToString());
            }        private bool CheckInput(string p)
            {
                return ! IsRegEx(@"[^- 0-9 A-Z a-z \u4e00-\u9fa5]" ,p);
            }        private bool IsRegEx(string regExValue ,string itemValue)
            {
                try
                {
                    Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
                    if (regex.IsMatch(itemValue)) return true;
                    else return false;
                }
                catch (Exception)
                {
                    return false;
                }
                finally
                {
                }
            }
        }
    }