11位数的正则表达式,前导零

时间:2014-09-14 13:41:19

标签: c# regex asp.net-mvc-4

我想验证最少11位数的文本框,使用正则表达式下面的正则表达式输入

^[0-9]{11}$

如果我输入任意数字为零,它只适用于非前导零位数我在模型中得到验证失败错误

我的行动

    [HttpPost]
    public ActionResult CreateClient(CompanyClient client)
    {
        var result = _dal.AddClient(client);
        if (result)
        {
            return Json(new { success = true }, JsonRequestBehavior.AllowGet);
        }
        return Json(new { success = false }, JsonRequestBehavior.AllowGet);
    }

我的DAL

public bool AddClient(CompanyClient client)
    {
        try
        {
            using (var context = new MyContext())
            {
                client.Status = StatusEnum.Enabled;
                client.CreatedOn = DateTime.Now;
                client.CreatedBy = CurrentUserName;
                context.ICompanyClient.Add(client);
                context.SaveChanges();
                return true;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }

我的班级

public class CompanyClient
{
    public int CompanyClientId { get; set; }
    [DisplayName("Client Name")]
    [Required(ErrorMessage = "Client Name is required")]
    public string ClientName { get; set; }

    [DisplayName("Company Email")]
    [Required(ErrorMessage = "Company Email  is required")]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                        @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                        @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
                        ErrorMessage = "Email is not valid")]
    public string CompanyEmailId { get; set; }

    //[DataType(DataType.PhoneNumber)]
    [DisplayName("Telephone No")]
    [Required(ErrorMessage = "Telephone No  is required")]
    [RegularExpression("^[0-9]{11,}", ErrorMessage = "Please enter at least 11 digits")]
    public long Tel { get; set; }

    [DisplayName("Fax No")]
    [RegularExpression("^[0-9]{11,}", ErrorMessage = "Please enter at least 11 digits")]
    public long? FaxNo { get; set; }
    [DisplayName("Mobile No")]
    [RegularExpression("^[0-9]{10}$", ErrorMessage = "Please enter at least 10 digits")]
    public long? MobileNo { get; set; }
    public string ContactPerson { get; set; }
    public string CompanyLocation { get; set; }
    public StatusEnum Status { get; set; }
    public DateTime? CreatedOn { get; set; }
    public string CreatedBy { get; set; }
    public DateTime? ModifiedOn { get; set; }
    public string ModifiedBy { get; set; }


}

2 个答案:

答案 0 :(得分:4)

对于最少11位数字,您需要使用以下模式,

^[0-9]{11,}$

DEMO

enter image description here

答案 1 :(得分:1)

^[0-9]{11,}

当我尝试时它起作用了..