其实除了可以用js调整,也可以用php调整啊,根据$_GET['t']的值输出不同的rule.
特意去看了下jQuery Validate插件,发现可以这样这么做:
<form id="myform">
<input name="someinput" id="someinput" type="text">
<button>submit</button>
</form>
javascript:
$('#myform').validate({rules:{
someinput:{
<?php if ($_GET['t']==1) :?>
required:true,
minlength:3,
maxlength:15,
number:true
<?php else :?>
required:true,
minlength:1,
maxlength:5,
number:true
<?php endif;?>
}
}});
或者这么做:
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
}
$(function(){
var t = getQueryVariable('t');
$.validator.addMethod("someinput", function(value) {
if(t==1){
return !(value===null || value.length < 2 || value.length > 20);
}else if(t==2){
return !(value===null || value.length < 5 || value.length > 12);
}
return false;
}, 'Please enter "someinput"!');
$('#myform').validate({rules:{
someinput:'someinput'
}});
});