Hi nadeem1218,
Mobile Number validation using Regular Expression.
Please refer below sample.
View
@model Sample_152008_RegularExpression.Models.PersonModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
.error { color: Red; }
</style>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.LabelFor(m => m.MobileNumber)
@Html.TextBoxFor(m => m.MobileNumber, "")
@Html.ValidationMessageFor(m => m.MobileNumber, "", new { @class = "error" })
<hr />
<input type="submit" value="Submit" />
}
</body>
</html>
Controllers
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(PersonModel person)
{
if (ModelState.IsValid)
{
// Validation success.
}
return View();
}
}
Models
public class PersonModel
{
[Display(Name = "Mobile Number: ")]
[Required(ErrorMessage = "Invalid Mobile Number.")]
[RegularExpression(@"^05[0-9]{8}$", ErrorMessage = "Invalid Mobile Number.")]
public string MobileNumber { get; set; }
}
Screenshot