本文介绍了如何全局定义验证功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
抱歉,但是我是C#的新手....
任何朋友可以告诉我如何定义文本框的验证,例如仅数字,char.only等....实际上,我的问题是我想定义这些函数.我需要以其他2形式....

请任何哥们告诉我我该怎么办......?


C#窗口应用程序

hello guys,
sorry but i m new in C#....
can any buddy tell me how to define validation for text box like numbers only,char.only etc.... actually my problem is that i want to define these function.in such way ....i can call this function directly as i need in different-2 form ....

please can any buddy give me idea what i have to do......?


C# window application

推荐答案


class ReqFunctions
{
  public static bool ValidateTextBox(string txtBoxValue, string validationType)
  {
    if(validatioType == "Number")
    {
      Regex isnumber = new Regex("[^0-9]");
      return !isnumber.IsMatch(txtBoxValue);
    }
    if(validationType == "Char")
    {
      Regex ischar = new Regex("^[a-zA-Z]+





在您的代码中,您可以将该函数称为




In your code you can call this function as

ReqFunctions.ValidateTextBox(txtTextbox1.Text);



您还可以检查 MSDN链接 [ ^ ].
最近,我也发现了这一点:仅数字和字符C#中的文本框验证! [ ^ ]



You can also check this MSDN link[^].
Recently I have found this also: Numbers and Characters only Textbox Validation in C# ![^]


这篇关于如何全局定义验证功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:56
查看更多