怎么判断textbox控件中的字符串?

时间:2020-12-08 20:18:39
想要判断textbox控件中的字符串类型(textbox控件中输入的是手机号码),怎么判断textbox控件中输入的手机号码是联通的号码(联通的号码是130,131,132,133,153,156开头的),还是移动(号码是134,135,136,137,138,139,157,158,159开头的)和电信的号码(号码是以0开头的)

8 个解决方案

#1


这个老老实实的做就是了嘛.

#2


不知道楼主是否是需要这个函数

/// 判断一个字符是否在字符串列表中, 相当于sql的IN
/// 用法: isInStr("153","130,131,132,133,153,156")  
public bool isInStr(string findStr, string strList)
{
bool isExist = false;
string[] s  = strList.Split(',');
for(int i = s.GetLowerBound(0); i<=s.GetUpperBound(0);i++)
{
if (s[i]==findStr)
{
isExist = true;
break;
}
}
return isExist;
}




测试:


private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(isInStr("133","131,132,133"))
{
this.Response.Write("成功了<br>");
}

if(!isInStr("a","b,c,d"))
{
this.Response.Write("又成功了");
}


}

#3


    private string JudgeTelType(string textStr)
    {
        string type = "";
        string threeChar = textStr.Trim().Substring(1,3);
        if (threeChar == "130" || threeChar == "131" || threeChar = "132" || threeChar = "133" || 
            threeChar = "153" || threeChar = "156")
        {
            type = "联通";
        }
            //134,135,136,137,138,139,157,158,159
        else if (threeChar == "134" || threeChar == "135" || threeChar = "136" || threeChar = "137"
                 || threeChar = "138" || threeChar = "139" || threeChar = "157" || threeChar = "158"
                 || threeChar = "159")
        {
            type = "移动";
        }
        else if (threeChar.Substring(1, 1) == "0")
        {
            type = "电信";
        }
        else
        {
            type = "输入电话号码不正确";
        }
    }

--
其实应该用正则表达式

#4


--纠正   
 private string JudgeTelType(string textStr)
    {
        string type = "";
        string threeChar = textStr.Trim().Substring(1,3);
        if (threeChar == "130" || threeChar == "131" || threeChar == "132" || threeChar == "133" ||
            threeChar == "153" || threeChar == "156")
        {
            type = "联通";
        }
            //134,135,136,137,138,139,157,158,159
        else if (threeChar == "134" || threeChar == "135" || threeChar == "136" || threeChar == "137"
                 || threeChar == "138" || threeChar == "139" || threeChar == "157" || threeChar == "158"
                 || threeChar == "159")
        {
            type = "移动";
        }
        else if (threeChar.Substring(1, 1) == "0")
        {
            type = "电信";
        }
        else
        {
            type = "输入电话号码不正确";
        }
        return type;
    }

#5


应该可以用正则表达式吧,我不太懂正则表达式,我想应该可以用的

#6



         Regex re = new Regex(@"^13[0-3]\d{8}|153\d{8}|156\d{8}$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('连通')</script>");
         }
         Regex re = new Regex(@"^13[4-9]\d{8}|15[7-9]\d{8}$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('移动')</script>");
         }
         Regex re = new Regex(@"^0\d$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('电信')</script>");
         }

#7



         Regex re; 
         re = new Regex(@"^13[0-3]\d{8}|153\d{8}|156\d{8}$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('连通')</script>");
         }
         re = new Regex(@"^13[4-9]\d{8}|15[7-9]\d{8}$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('移动')</script>");
         }
         re = new Regex(@"^0\d$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('电信')</script>");
         }

#8


电信的有的带“-”,有的不带,可以上网搜索这个``

#1


这个老老实实的做就是了嘛.

#2


不知道楼主是否是需要这个函数

/// 判断一个字符是否在字符串列表中, 相当于sql的IN
/// 用法: isInStr("153","130,131,132,133,153,156")  
public bool isInStr(string findStr, string strList)
{
bool isExist = false;
string[] s  = strList.Split(',');
for(int i = s.GetLowerBound(0); i<=s.GetUpperBound(0);i++)
{
if (s[i]==findStr)
{
isExist = true;
break;
}
}
return isExist;
}




测试:


private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(isInStr("133","131,132,133"))
{
this.Response.Write("成功了<br>");
}

if(!isInStr("a","b,c,d"))
{
this.Response.Write("又成功了");
}


}

#3


    private string JudgeTelType(string textStr)
    {
        string type = "";
        string threeChar = textStr.Trim().Substring(1,3);
        if (threeChar == "130" || threeChar == "131" || threeChar = "132" || threeChar = "133" || 
            threeChar = "153" || threeChar = "156")
        {
            type = "联通";
        }
            //134,135,136,137,138,139,157,158,159
        else if (threeChar == "134" || threeChar == "135" || threeChar = "136" || threeChar = "137"
                 || threeChar = "138" || threeChar = "139" || threeChar = "157" || threeChar = "158"
                 || threeChar = "159")
        {
            type = "移动";
        }
        else if (threeChar.Substring(1, 1) == "0")
        {
            type = "电信";
        }
        else
        {
            type = "输入电话号码不正确";
        }
    }

--
其实应该用正则表达式

#4


--纠正   
 private string JudgeTelType(string textStr)
    {
        string type = "";
        string threeChar = textStr.Trim().Substring(1,3);
        if (threeChar == "130" || threeChar == "131" || threeChar == "132" || threeChar == "133" ||
            threeChar == "153" || threeChar == "156")
        {
            type = "联通";
        }
            //134,135,136,137,138,139,157,158,159
        else if (threeChar == "134" || threeChar == "135" || threeChar == "136" || threeChar == "137"
                 || threeChar == "138" || threeChar == "139" || threeChar == "157" || threeChar == "158"
                 || threeChar == "159")
        {
            type = "移动";
        }
        else if (threeChar.Substring(1, 1) == "0")
        {
            type = "电信";
        }
        else
        {
            type = "输入电话号码不正确";
        }
        return type;
    }

#5


应该可以用正则表达式吧,我不太懂正则表达式,我想应该可以用的

#6



         Regex re = new Regex(@"^13[0-3]\d{8}|153\d{8}|156\d{8}$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('连通')</script>");
         }
         Regex re = new Regex(@"^13[4-9]\d{8}|15[7-9]\d{8}$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('移动')</script>");
         }
         Regex re = new Regex(@"^0\d$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('电信')</script>");
         }

#7



         Regex re; 
         re = new Regex(@"^13[0-3]\d{8}|153\d{8}|156\d{8}$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('连通')</script>");
         }
         re = new Regex(@"^13[4-9]\d{8}|15[7-9]\d{8}$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('移动')</script>");
         }
         re = new Regex(@"^0\d$");
         if (re.IsMatch(TextBox1.Text))
         {
             Page.RegisterStartupScript("s", "<script>alert('电信')</script>");
         }

#8


电信的有的带“-”,有的不带,可以上网搜索这个``