C#验证:正则表达式 验证类和界面处理(手机号码,邮箱,IP地址)

使用步骤:

1.创建验证类

2.界面调用验证类中的方法进行验证

实例代码演示如下

1.创建验证类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Mobot.TeamFoundation.Client.UI.Users
{
    public class Validator
    {
        /// <summary>
        /// 验证手机号码
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsMobilePhone(string source)
        {
            return Regex.IsMatch(source, @"^1[358]\d{9}$", RegexOptions.IgnoreCase);
        }
        /// <summary>
        /// 验证邮箱
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsEmail(string source)
        {
            return Regex.IsMatch(source, @"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@" +
            @"([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", RegexOptions.IgnoreCase);
        }
    }
}

2.界面调用验证类中的方法进行验证

private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text)||this.txtUserName.Text.Trim().Length>50)
            {
                XtraMessageBox.Show("用户名不能为空,且字符不能超过50个!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUserName.Focus();
                txtUserName.SelectAll();
                return;
            }
            if (string.IsNullOrEmpty(txtDisplayName.Text) || this.txtDisplayName.Text.Trim().Length > 50)
            {
                XtraMessageBox.Show("显示名不能为空,且字符不能超过50个!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtDisplayName.Focus();
                txtDisplayName.SelectAll();
                return;
            }
            if (!Validator.IsMobilePhone(txtPhoneNumber.Text.Trim()))
            {
                XtraMessageBox.Show("请检查手机号是否输入有误!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cbbRoleId.Focus();
                this.txtPhoneNumber.Focus();
                this.txtPhoneNumber.SelectAll();
                return;
            }
            if (!Validator.IsEmail(this.txtEmail.Text.Trim()))
            {
                MessageBox.Show("邮箱地址格式不正确!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtEmail.Focus();
                this.txtEmail.SelectAll(); ;
                return;
            }
            if (this.cbbRoleId.SelectedIndex == -1)
            {
                MessageBox.Show("请选择角色!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbbRoleId.Focus();
                return;
            }

        }

 杂记:下面是本人的邮箱另一种简单验证,可行性还求高人指点,是否可行?

if (!this.txtEmail.Text.Trim().Contains("@"))
            {
                MessageBox.Show("邮箱地址格式不正确!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtEmail.Focus();
                this.txtEmail.SelectAll(); ;
                return;
            }
            if (this.txtEmail.Text.Trim().Length == 0 || this.txtEmail.Text.Trim().Length > 50)
            {
                MessageBox.Show("邮箱不能为空,且字符不能超过50个!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtEmail.Focus();
                this.txtEmail.SelectAll(); ;
                return;
            }
            if (!Validator.IsEmail(this.txtEmail.Text.Trim()))
            {
                MessageBox.Show("邮箱地址格式不正确!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtEmail.Focus();
                this.txtEmail.SelectAll(); ;
                return;
            }

 补充:验证服务端IP @2013.01.29

 /// <summary>
        /// 验证服务端IP
        /// </summary>
        /// <returns></returns>
        public static bool ValidateIpAddress(string ip, out string msg)
        {
            msg = string.Empty;
            string strReg = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(strReg);
            if (!regex.IsMatch(ip))
            {
                msg = "配置的IP地址格式不正确!";
                return false;
            }
            return true;
        }

 

 

 

posted @ 2012-12-17 10:17  乡香田甜  阅读(742)  评论(1编辑  收藏  举报