![](/img/trans.png)
[英]Regular Expression accepting only 9 digits, no special characters , may have leading zero's only up to 2
[英]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; }
}
嘗試
^[0-9]{11,}
當我嘗試時它奏效了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.