When I entered value for email in textbox it didn't fire VerifyUserEmail method. Below is my code. Where is my mistake?
View
<div class="form-group">
@Html.LabelFor(model => model.User_Email, new { @class = "control-label col-lg-2" })
<div class="col-lg-4">
@Html.EditorFor(model => model.User_Email)
@Html.ValidationMessageFor(model => model.User_Email)
</div>
</div>
Model
[DisplayName("Email")]
[Remote("VerifyUserEmail", "User", ErrorMessage = "Email already exists!")]
public string User_Email { get; set; }
Controller
public JsonResult VerifyUserEmail(string User_Email)
{
try
{
using (RKDYMEntities objConnection = new RKDYMEntities())
{
ObjectParameter objIErrorCode = new ObjectParameter("ErrorCode", typeof(Int32));
ObjectParameter objBFlag = new ObjectParameter("bFlg", typeof(bool));
objConnection.Check_User_Exists(User_Email, objBFlag, objIErrorCode);
if (Convert.ToBoolean(objBFlag.Value) != true)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
}
catch (Exception Ex)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
}
I have also added <add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
in web.config.
Below is generated html code:
<div class="form-group">
<label class="control-label col-lg-2" for="User_Email">Email</label>
<div class="col-lg-4">
<input class="text-box single-line" data-val="true" data-val-length="The field Email must be a string with a maximum length of 200." data-val-length-max="200" data-val-remote="Email already exists!" data-val-remote-additionalfields="*.User_Email" data-val-remote-url="/User/VerifyUserEmail" id="User_Email" name="User_Email" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="User_Email" data-valmsg-replace="true"></span>
</div>
</div>
Jquery Validation
I am using client side JQuery validation which is working fine.
$(document).ready(function () {
$("#Registration").validate({
rules: {
User_Email: {
required: true, email: true
},
User_Password: {
required: true,
minlength: 8
},
User_ReTypePassword: {
required: true,
minlength: 8,
equalTo: '#User_Password'
},
User_FirstName: { required: true },
User_LastName: { required: true },
User_ZipCode: {
required: true,
digits: true
}
},
messages: {
User_Email: {
required: 'Email is required', email: 'Invalid email'
},
User_Password: {
required: 'Password is required',
minlength: 'Min length of password is 8'
},
User_ReTypePassword:
{
required: 'Confirm Password is required',
equalTo: 'Confirm Password must be equal to password'
},
User_ZipCode: { required: 'ZipCode is required', digits: 'Only digits are allowed' },
User_FirstName: { required: 'First Name is required' },
User_LastName: { required: 'Last Name is required' }
},
errorPlacement: function (error, element) {
error.insertAfter(element);
},
showErrors: function (errorMap, errorList) {
this.defaultShowErrors();
}
});
});
Edit
I have removed
$("#Registration").validate({
// rules: {
and it's working.
Any idea? Why?
The error message from the data attribute is never used (You can test this by using my simple example below):
[Remote("VerifyUserEmail", "User"]
Remote accepts an overload with an error message but does nothing with it, you should return the error message from the server-side.
Basically anything other than a true return is the error message sent back to the client. If you add the message instead of returning false you should see this.
return Json(string.Format("{0} does not exist.", User_Email),
JsonRequestBehavior.AllowGet);
You also need to ensure you have the following references:
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
Simple working example
Model
public class AModel
{
[DisplayName("Email")]
[Remote("VerifyUserEmail", "Home")]
public string User_Email { get; set; }
}
Controller:
public JsonResult VerifyUserEmail(string User_Email)
{
if (User_Email == "1")
{
return Json("No", JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
View
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
<div class="form-group">
@Html.LabelFor(model => model.User_Email, new { @class = "control-label col-lg-2" })
<div class="col-lg-4">
@Html.EditorFor(model => model.User_Email)
@Html.ValidationMessageFor(model => model.User_Email)
</div>
</div>
<input type="submit" />
}
Screen shot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With