簡體   English   中英

11位數字的正則表達式,前導零

[英]Regular Expression for 11 digits number with leading zero

我想驗證至少用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; }


}

至少11位數字,您需要使用以下格式,

^[0-9]{11,}$

DEMO

在此處輸入圖片說明

嘗試

^[0-9]{11,}

當我嘗試時它奏效了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM