Akshay Chawla
Akshay Chawla

Reputation: 613

Email format validation issue

Model code

[Required(ErrorMessage = "Required.")]
[EmailAddress(ErrorMessage = "Invalid email address.")]
[Remote("checkEmailAvailability", "Users", "Users", AdditionalFields = "RoleId,UserID", ErrorMessage = "Email address already in use")]
public string stEmail { get; set; }

View code

@Html.TextBoxFor(model => model.stEmail, null, new { @class = "form-control" })

<script src="@Url.Content("~/assets/global/plugins/jquery.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/assets/global/plugins/jquery-validation/js/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Areas/Users/Scripts/AddPatients.js")" type="text/javascript"></script>

Js Code [ AddPatients.js ]

$("#btnAddPatient").click(function (event) {
    event.preventDefault();
    var $form = $('#frmAddPatient');
    if ($form.valid()) {//some code
    }
    }
});

Controller Code

[HttpPost]
public JsonResult updatePatient(AddEditPatientViewModel foPatientViewModel)
{
 if (ModelState.IsValid)
  {
    //some code
  }
}

What issue i am facing is that when user enters 'abc@gmail' in email address textbox it is getting pass from '$form.valid()' but 'ModelState.IsValid' is returning false ["Invalid email address."] for that same value, so user is not able to post the form.

So can anyone tell me whats wrong in my code or any solution for this?
Thanks in advance.

Upvotes: 0

Views: 93

Answers (1)

Tomato32
Tomato32

Reputation: 2245

Try Html.EditorFor helper method instead of Html.TextBoxFor. Hope to help, my friend :))

Upvotes: 1

Related Questions