当文档加载时,它执行checkseason()
函数,然后忽略其余的代码。如果加载时没有checkseason()
,代码会因某种原因正常运行,我需要在开头执行checkseason()
以将验证规则应用于输入字段。
$.mask.definitions['~'] = '[+-]';
$.mask.definitions['x'] = '[0-9]';
$.mask.definitions['*'] = '[-A-Za-z0-9._\\\/?&=~]';
function checkseason(){
d = document.getElementById("season").value;
$('#second_form').validate({
rules: {
patch: {
required: true,
minlength: 4,
maxlength: 4,
remote: {
url: "../checkpatch.php",
type: "post"
}
}
},
messages: {
patch: {
required: "Please enter patch version.",
remote: "Patch with this number already exists."
}
},
});
$('select[name="season"]').on('change', function () {
if ($(this).val() == "newseasons") {
$('input[name="patch"]').rules('add', {
minlength: 4,
maxlength: 4,
messages: {
minlength: "Please enter 3 numbers"
}
});
} else if ($(this).val() == "oldseasons") {
$('input[name="patch"]').rules('add', {
minlength: 8,
maxlength: 9,
messages: {
minlength: "Please enter 8 numbers"
}
});
}
});
if(d==='newseasons'){
$("#patch").mask("x.xx", {
placeholder: ""
});
}
else if(d==='oldseasons'){
$("#patch").unmask();
$("#patch").mask("?x.x.x.xxx", {
placeholder: ""
});
}
}
$(document).ready(function(){
checkseason();
var config = {
fillAll: true
};
document.forms["second_form"].oninput = function(e) {
var champion = this["champion[]"];
//Force into array
if (champion[0] === undefined)
champion = [this["champion[]"]];
var reached = {
valid: 0,
unique: 0
};
var inputs = [].map.call(champion, function(n) {
return n.value
}).filter(function(n) {
return n.length
});
var valid = [].every.call(champion, function(n, i) {
n.setCustomValidity("");
if (config.fillAll) return (reached.valid = i, champions.hasOwnProperty(n.value));
else return n.value ? (
reached.valid = i,
champions.hasOwnProperty(n.value) > -1
) : true;
});
var unique = inputs.slice(0).sort().every(function(n, i, a) {
reached.unique = inputs.lastIndexOf(n);
return n != a[i - 1];
});
//Check for valid champions
if (!valid) {
champion[reached.valid].setCustomValidity("This is not a valid champion, please correct this field and resubmit.")
}
//Check for duplicates
if (!unique) {
champion[reached.unique].setCustomValidity("This champion has already been entered.")
}
this.checkValidity();
};
});