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)
{
<b>
<span style="font-family: Verdana; font-size: 10pt;">
Name : @ViewBag.Name<br />
Email : @ViewBag.Email<br />
</span>
</b>
}
}
<br />
<br />
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div>
<fieldset>
<legend>Employee</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name, "", new { @Style = "color:red" })
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email, "", new { @Style = "color:red" })
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password, new { maxlength = 20, value = ViewBag.Selpwd })
@Html.ValidationMessageFor(model => model.Password, "", new { @Style = "color:red" })
</div>
<p>
<input id="btnAdd" type="SUBMIT" value="Register" onclick="AddTextBox()" />
</p>
<div id="TextBoxContainer">
<!--Textboxes will be added here -->
</div>
</fieldset>
</div>
}
<script type="text/javascript">
function GetDynamicTextBox(value){
return '<input name = "DynamicTextBox" type="text" value = "' + value + '" />'
}
var count=0;
function AddTextBox()
{
if (count <= 1) {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicTextBox("");
document.getElementById("TextBoxContainer").appendChild(div);
document.getElementById('btnAdd').click();
}
}
</script>
@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);
}
}