Hi Frnds
i am working on client side validation in mvc4..
i worked with this and this code is working fine..
now i added 1 textbox at runtime for ,if i entered the wrong details then textbox should be generated..
this thing also done...means if i entered the wrong details textbox is diaplayong...this is correct..
after that if i fillup the correct details and click on submit button textbox should not be display..
this thing also did..
but i am confused if i click on submit button textbox should not be appear but it is appear then gone..
this is my code///
model///
public class Employee
{
//[Required(ErrorMessage = "Name is Required")]
[Required]
[Display(Name = "Name")]
[RegularExpression(@"^.{6,10}$", ErrorMessage = "Name should be of minimum 6 and maximum 10 characters")]
public string Name { get; set; }
[Required(ErrorMessage = "Email is Required")]
[Display(Name = "Email")]
[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 Email { get; set; }
[Required(ErrorMessage = "Password is Required")]
[Display(Name = "Password")]
[RegularExpression(@"^(?=.*\d)(?=.*[a-zA-Z]).{4,8}$", ErrorMessage = "Password Should be contain atleast 1 Upper, 1 Lower and 1 number")]
//Password Should be contain atleast 1 Upper, 1 Lower and 1 Special Character
public string Password { get; set; }
}
View//
@model ClientSideValidation.Models.Employee
@{
ViewBag.Title = "Employee Registration Form";
}
@if (ViewData.ModelState.IsValid)
{
if (@ViewBag.Name != null)
{
Name : @ViewBag.Name
Email : @ViewBag.Email
}
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
controller//
public class EmployeeRegFormController : Controller
{
// GET: EmployeeRegForm
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(Employee model)
{
if (ModelState.IsValid)
{
ViewBag.Name = model.Name;
ViewBag.Email = model.Email;
ViewBag.Password = model.Password;
}
if(model.Name == null)
{
ModelState.AddModelError("", "Details is incorrect!");
}
return View(model);
}
}
Vinay SinghPosted Sep 6, 2016, 3:09 AM