Hi, I’m trying to create a validation that prevents the page from being submitted if the values are not correct. I’ve tried a client side validation using this example:
http://www.codeproject.com/Articles/275056/Custom-Client-Side-Validation-in-ASP-NET-MVC3. The validation seems to work but only after the page is submitted then when the page returns other information like the array of dropdown lists have the same values.
I believe that if I prevent the page from being submitted in the first place this may solve my problem.
My question: If the page does not pass validation, how can I use prevent the page from being submitted after the user click the save button?
Here’s my model:
[Schoolin.Models.CheckPreGrade]
[Required(ErrorMessage="This is a required field")]
public virtual int? preGrade { get; set; }
//My [Required] attribute does not work for some reason
Here’s my validation class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Schoolin.Models
{
public class CheckPreGrade : ValidationAttribute, IClientValidatable
Like I said, it works but the page disappears then returns with the error message but other controls have worn info. My goal is to not allowed to be submitted so that the information on the form does not change.
it looks close. in the source are the vaidation attributes rendered. are the proper javascript script files included. if you set a javascript breakpoint does the adapter add fire? does the validation method get fired?
if you read the jquery validation docs, it pretty easy to understand how every is wired up. first check the method added. you can get the rules and see if your rule is there (get the field and check its rules collection). see the add rule source code to
understand.
The validation methods are fired. the validation happens as expected. The problem is although nothing gets posted to the database, the page goes away and when it comes back the validation worked as expected but other controls on the view act strangly.
How can i call the validation as the uaer leaves the field rather than waiting for until the save burton is clicked?
It seems that your validation are fired server side so that page is validate and get bac with validation message.
You need to prevent page submission if any of your validation failed. You can handle submit button's click event and
return false if any of validation failed.
1) check whether your web.config contains following keys/not <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 2) check whether you project have following script files jquery.validate.unobtrusive.min.js
jquery.validate.min.js and refernced in layout.cshtml Thanks, Ashok
Seeing that my validation works but only after clicking the button, is there an event that fires after the cursor leaves the control? And then I can perhaps fire the validation inside of that event?
AlexanderBla...
Member
325 Points
309 Posts
how can I use prevent the page from being submitted if it doesn't pass validation?
Dec 09, 2012 09:29 PM|LINK
Hi, I’m trying to create a validation that prevents the page from being submitted if the values are not correct. I’ve tried a client side validation using this example: http://www.codeproject.com/Articles/275056/Custom-Client-Side-Validation-in-ASP-NET-MVC3. The validation seems to work but only after the page is submitted then when the page returns other information like the array of dropdown lists have the same values. I believe that if I prevent the page from being submitted in the first place this may solve my problem.
My question: If the page does not pass validation, how can I use prevent the page from being submitted after the user click the save button?
Here’s my model:
[Schoolin.Models.CheckPreGrade]
[Required(ErrorMessage="This is a required field")]
public virtual int? preGrade { get; set; }
//My [Required] attribute does not work for some reason
Here’s my validation class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Schoolin.Models
{
public class CheckPreGrade : ValidationAttribute, IClientValidatable
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value==null){value=0;};
int intPutNum = (int)value;
string sErrorMessage = "Something greater than 12";
if (!(intPutNum > 12)) { return new ValidationResult(sErrorMessage); }
return ValidationResult.Success;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule ruleTwo = new ModelClientValidationRule();
ruleTwo.ValidationType = "checkpregrade";
ruleTwo.ErrorMessage = "Something greater than 12";
ruleTwo.ValidationParameters.Add("param", "12");
return new List<ModelClientValidationRule> { ruleTwo };
}
}
}
Here’s my javaScript class:
jQuery.validator.addMethod("checkpregrade",
function (value, element, param) {
var currNum = param;
var inPutNumber = value;
var ckGrade = getAge(inPutNumber, currNum);
var result = false;
if (ckGrade >12) { result = true; }
return result;
});
jQuery.validator.unobtrusive.adapters.add("checkpregrade", ["param"], function (options) {
options.rules["checkpregrade"] = options.params.param;
options.messages["checkpregrade"] = options.message;
});
function getAge(inPutNumber, currNum) {
return currNumber.valueOf() - inPutNumber.valueOf();
}
Like I said, it works but the page disappears then returns with the error message but other controls have worn info. My goal is to not allowed to be submitted so that the information on the form does not change.
bruce (sqlwo...
All-Star
36816 Points
5438 Posts
Re: how can I use prevent the page from being submitted if it doesn't pass validation?
Dec 10, 2012 01:11 AM|LINK
it looks close. in the source are the vaidation attributes rendered. are the proper javascript script files included. if you set a javascript breakpoint does the adapter add fire? does the validation method get fired?
if you read the jquery validation docs, it pretty easy to understand how every is wired up. first check the method added. you can get the rules and see if your rule is there (get the field and check its rules collection). see the add rule source code to understand.
AlexanderBla...
Member
325 Points
309 Posts
Re: how can I use prevent the page from being submitted if it doesn't pass validation?
Dec 10, 2012 03:42 AM|LINK
The validation methods are fired. the validation happens as expected. The problem is although nothing gets posted to the database, the page goes away and when it comes back the validation worked as expected but other controls on the view act strangly. How can i call the validation as the uaer leaves the field rather than waiting for until the save burton is clicked?
dmsolanki
Member
162 Points
80 Posts
Re: how can I use prevent the page from being submitted if it doesn't pass validation?
Dec 10, 2012 10:02 AM|LINK
Hello,
It seems that your validation are fired server side so that page is validate and get bac with validation message.
You need to prevent page submission if any of your validation failed. You can handle submit button's click event and return false if any of validation failed.
I hope I understand your problem.
AlexanderBla...
Member
325 Points
309 Posts
Re: how can I use prevent the page from being submitted if it doesn't pass validation?
Dec 11, 2012 05:28 AM|LINK
ashok4v
Member
220 Points
58 Posts
Re: how can I use prevent the page from being submitted if it doesn't pass validation?
Dec 11, 2012 11:48 AM|LINK
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
AlexanderBla...
Member
325 Points
309 Posts
Re: how can I use prevent the page from being submitted if it doesn't pass validation?
Dec 11, 2012 09:33 PM|LINK
//these files are in my web.config:
<appSettings>
<add key="webpages:Version" value="1.0.0.0"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
//I have both of these in the layout file:
<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>
Seeing that my validation works but only after clicking the button, is there an event that fires after the cursor leaves the control? And then I can perhaps fire the validation inside of that event?