问题
I'm using jQuery tools validator for my web app in ASP.net MVC4. I'm trying to validate dropdownbox using validator without success.
I've gone through this question, but it dosen't solved my problem.
my code is as below.
$.tools.validator.fn("select[required=required]", function(input, value) {
// If the first item in the list is selected return FALSE
alert("Inside required of dropdown");
if(input[0].options[0].selected)
{
alert("returning false");
en: "Please select value from this drop down"
}
else
{
alert("returning true");
return true;
}
});
it does alerts all the alert but it dosen't show error message on false, rather on selecting element it does fires selected item's value as validation message.
any help would be appriciated.
回答1:
Problem was with my html code, actually I assigned required="required" but when i done some insight in tools.validator.js, I found function to handle required as following
note : I've downloaded custom jquery.tools.min.js from jQuery Tools with validator only.
g.fn("[required]", "Please complete this mandatory field.", function (a, b) {
if (a.is(":checkbox")) return a.is(":checked");
return b
Above function is located at line# 140 in custom jquery.tools.min.js.
More easy to understand version can be downloaded from here.
so, when i assign required="required", than "required" is passed as value to the function in arugument b, and same was returned which was causing the problem.
so the solution is to pass required="true" or required="false", so "true"/"false" will be passed to function in argument b and same will b returned.
来源:https://stackoverflow.com/questions/13210017/dropdown-box-required-validation-using-jquery-tools-validator