我想验证文本框与最少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; }
}发布于 2014-09-14 21:42:47
对于至少11位数字,您需要使用以下模式,
^[0-9]{11,}$DEMO

发布于 2014-09-14 22:18:17
试一试
^[0-9]{11,}当我尝试的时候它起作用了..
https://stackoverflow.com/questions/25833885
复制相似问题