在一个TextBox输入框中我想要用户只能输入数字,不能输入英文字母和中文,限制输入英文字母已控制住,但是用户可以通过输入法输入汉字,请问怎样直接控制不让输入汉字?(不采用通过在焦点离开输入框的时候验证输入的内容,给错误提示的方法!)

解决方案 »

  1.   

    用正则表达式,可以把文本框中非阿拉伯数字的字符自动消除
    文本框id.Attributes.Add("onfocusout", "value=value.replace(/[^\\0-9]/g,'')");
      

  2.   

    MS有一段用正则表达式过滤的程序,你可以参考
    //*********************************************************************
    //
    // <说明>
    // 使用正则表达式校验输入的文本内容并用空字符串替换文本中的字符
    // 移除文本中所有不在[a-zA-Z0-9_]范围内的字符 
    // <说明>
    // <res>
    // For a good reference on Regular Expressions, please see
    //  - http://regexlib.com
    //  - http://py-howto.sourceforge.net/regex/regex.html
    // </res>
    // <param name="inputText">The text to validate.</param>
    // <returns>Sanitized string</returns>
    //
    //*********************************************************************public static string CleanStringRegex(string inputText)
    {
    RegexOptions options = RegexOptions.IgnoreCase;
    return ReplaceRegex(inputText,@"[^\\.!?""',\-\w\s@]",options);
    }//*********************************************************************
    //
    // <summary>
    // Removes designated characters from an input string input text using a Regular Expression.
    // </summary>
    // <res>
    // For a good reference on Regular Expressions, please see
    //  - http://regexlib.com
    //  - http://py-howto.sourceforge.net/regex/regex.html
    // </res>
    // <param name="inputText">The text to clean.</param>
    // <param name="regularExpression">The regular expression</param>
    // <returns>Sanitized string.</returns>
    //
    //*********************************************************************private static string ReplaceRegex(string inputText, string regularExpression, RegexOptions options)
    {
    Regex regex = new Regex(regularExpression,options);
    return regex.Replace(inputText,"");
    }