请教各位大虾:
请问如何判断输入的数据为英文字母,汉字或者是数字?

解决方案 »

  1.   

    private const string IS_NUMBER = @"(^[0-9]+$)"; 
    private const string IS_YYYYMM_ICON = @"(^[1][8-9][0-9][0-9]/[0][1-9]+$)|(^[1][8-9][0-9][0-9]/[1][0-2]+$)|(^[2-9][0-9][0-9][0-9]/[0][1-9]+$)|(^[2-9][0-9][0-9][0-9]/[1][0-2]+$)";
    private const string IS_YYYYMM = @"(^[1][8-9][0-9][0-9][0][1-9]+$)|(^[1][8-9][0-9][0-9][1][0-2]+$)|(^[2-9][0-9][0-9][0-9][0][1-9]+$)|(^[2-9][0-9][0-9][0-9][1][0-2]+$)";
    private const string IS_HALF_WORD = @"(^(([!-/:-@[-`{-~])|([a-zA-Z0-9]))+$)"; public ComStyleCheck()
    {
    } /// <summary>
    /// regular expression check function
    /// </summary>
    /// <param name="regEx">expression</param>
    /// <param name="itemValue">check item</param>
    /// <returns>OK : true / NG : false</returns>
    //Regular Expression check
    public static bool IsRegEx(string regExValue, string itemValue) 
    {
    try 
    {
    //Get Regular Expression
    Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
    if (regex.IsMatch(itemValue))
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    catch (Exception) 
    {
    return false;
    }
    } /// <summary>
    /// maxlength check
    /// </summary>
    /// <param name="textForCheck"></param>
    /// <param name="maxLength"></param>
    /// <returns></returns>
    public static bool CheckStringLengthValid(string textForCheck,int maxLength)
    {
    int length = 0;
    char c; for(int i = 0; i < textForCheck.Length; i++)
    {
    c = textForCheck[i];
    if (( c < 128 ) || ((c > 65376 )&&(c < 65440 )))
    {
    length++;
    }
    else
    {
    length +=2;
    }
    } if(length > maxLength)
    {
    return false;
    }
    else
    {
    return true;
    }
    } #region number, decimal, money check
    /// <summary>
    /// is nember
    /// </summary>
    /// <param name="itemValue">check text</param>
    /// <returns>OK : true / NG : false</returns>
    //Number Check
    public static bool IsNumber(string itemValue) 
    {
    return (IsRegEx(IS_NUMBER, itemValue));
    } /// <summary>
    ///  is Decimail
    /// </summary>
    /// <param name="decItem"></param>
    /// <returns></returns>
    //Decimail Check
    public static bool IsDecimail(string decItem)
    {
    try
    {
    decimal.Parse(decItem);
    }
    catch
    {
    return false;
    } return true;
    } //Check Money Value
    public static bool CheckMoneyValue(string sourceText, int numberPoint, int decimalPoint)
    {
    double d; if ( decimalPoint < 0 )
    {
    return false;
    }
    else
    {
    if(sourceText.StartsWith(".") || sourceText.EndsWith("."))
    {
    return false;
    }
    //Number Styles Check
    if ( Double.TryParse(
    sourceText, 
    System.Globalization.NumberStyles.Any, 
    System.Globalization.NumberFormatInfo.InvariantInfo, out d) 
    )
    {
    //check Value About decimalPoint
    string checkValue = sourceText.Replace(",", ""); if ( decimalPoint > 0 && checkValue.IndexOf('.')>0)
    {
    if ( (checkValue.Split('.'))[0].Length > numberPoint || 
    (checkValue.Split('.'))[1].Length > decimalPoint
    )
    {
    return false;
    }
    }
    else
    {
    //if xxx.00 
    if(checkValue.EndsWith(".00"))
    {
       numberPoint = numberPoint + 3;
    } if ( checkValue.Length > numberPoint )
    {
    return false;
    }
    }
    }
    else
    {
    return false;
    }
    } return true;
    }
      

  2.   

    validator控件里写正则表达式比如:
    只能输入英文,中文,数字,下划线和短横线 
    ^[-_a-zA-Z0-9\u4e00-\u9fa5]+$