正则表达式验证电话号码格式
private void button1_Click(object sender, EventArgs e)
{
if (!IsTelephone(textBox1.Text))//验证电话号码格式是否正确 { MessageBox.Show("电话号码格式不正确"); }//弹出消息对话框 else { MessageBox.Show("电话号码格式正确"); }//弹出消息对话框
}
///
/// 验证电话号码格式是否正确
///
/// 电话号码信息
/// 方法返回布尔值
public bool IsTelephone(string str_telephone)
{
return //使用正则表达式判断是否匹配
Regex.IsMatch(str_telephone, @"^(d{3,4}-)?d{6,10}$");
}
点击‘电话验证’,进入 IsTelephone(文本内容)方法使用正则表达式判断格式再返回给点击事件方法,根据返回结果提示
![fb116ce0637a396ed168ab852a333295.png](https://img-blog.csdnimg.cn/img_convert/fb116ce0637a396ed168ab852a333295.png)