I need to validate an input field value from user before the form is submitted.
I have created an action in my custom controller and decorated the field with it:
action name: CheckValue
controller name: Validate
[Remote("CheckValue", "Validate"), ErrorMessage="Value is not valid"] public string Value { get; set; }
The problem is when I press submit, the form is being submitted and then the message Value is not valid
is shown if the value entered by the user is not valid.
How can I validate the value entered by user and prevent the form to be submitted if value is not valid, and display the error message?
If I try in JavaScript to check if the form is valid $("#formId").valid()
that returns true, that means no matter what is the status of the value (valid or not) the form is valid.
In the other hand if I decorate another field with the [Required]
attribute the form is not submitted and the error is shown for that field that is required. However the validation doesn't occur behind the scene for the remote validation field.
ASP.NET MVC 3 provides a mechanism that can make a remote server call in order to validate a form field without posting the entire form to the server. This is useful when you have a field that cannot be validated on the client and is therefore likely to fail validation when the form is submitted.
Remote Validation is a technique that uses client side script to validate user input on the server without posting the entire form. It is typically used to compare the user input with a dynamic list of values. One example of its use would be to prevent duplicate user names being submitted.
ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .
Remote is the attribute for validation in Data Annotation, which is used in model class to validate records instantly.
The complete solution of Remote Validation in MVC. It will check if the email exists in database and show the following error:
Email already exists
Account Controller Action
[AllowAnonymous] [HttpPost] public ActionResult CheckExistingEmail(string Email) { try { return Json(!IsEmailExists(Email)); } catch (Exception ex) { return Json(false); } } private bool IsEmailExists(string email) => UserManager.FindByEmail(email) != null;
Add Model Validation
[Required] [MaxLength(50)] [EmailAddress(ErrorMessage = "Invalid Email Address")] [System.Web.Mvc.Remote("CheckExistingEmail", "Account", HttpMethod = "POST", ErrorMessage = "Email already exists")] public string Email { get; set; }
Add Scripts
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
You dont put the Controller code. But must to be something like this:
Your code:
[Remote("CheckValue", "Validate", ErrorMessage="Value is not valid")] public string Value { get; set; }
My code for the controller(Validate):
public ActionResult CheckValue(string Value) { if (Value == "x value") { // This show the error message of validation and stop the submit of the form return Json(true, JsonRequestBehavior.AllowGet); } else { // This will ignore the validation and the submit of the form is gone to take place. return Json(false, JsonRequestBehavior.AllowGet); } }
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