我的html表单有一些验证输入:
<form role="form" id="addForm" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="userName">User name</label>
<input type="text" class="form-control" required name="userName" id="userName" placeholder="Enter user name">
</div>
<div class="form-group ">
<label for="email">Email</label>
<input class="form-control" required id="email" name="email" placeholder="Enter email" type="email"/>
</div>
<div class="form-group ">
<label for="homePage">Home page</label>
<input class="form-control" id="homePage" name="homePage" placeholder="Enter home page" type="url"/>
</div>
<div class="form-group ">
<label for="text">Text</label>
<textarea class="form-control" required id="text" rows="8" name="text" placeholder="Enter text"></textarea>
</div>
<a href ="javascript:void(0);" id="captchaLink"><img id="secPic" src="sec_pic.php" alt="secret code"></a>
<div class="form-group ">
<label for="captcha">Captcha</label>
<input class="form-control" required id="captcha" name="captcha" placeholder="Enter captcha"/>
</div>
<input type="hidden" name="userIp" id="userIp" value="<?php echo $_SERVER['REMOTE_ADDR']?>">
<input type="hidden" name="userBrowser" id="userBrowser" value="<?php echo $_SERVER['HTTP_USER_AGENT'] ?>">
<div class="form-group ">
<label for="userFile">File</label>
<input name="userFile" id="userFile" type="file" />
</div>
<button name="submit" id="submit" value="" type="submit" class="btn btn-large btn-primary btn-block">Add a news</button>
</form>
对于验证,我使用Jquery验证插件。 我的js是:
jQuery.validator.addMethod("lettersonly", function (value, element) {
return this.optional(element) || /^[a-z0-9_-]+$/i.test(value);
}, "Please use only a-z0-9_-");
jQuery.validator.addMethod("fileSize", function (val, element) {
var size = element.files[0].size;
console.log(size);
if (size > 100000)// checks the file more than 100 kb
{
console.log("returning false");
return false;
} else {
console.log("returning true");
return true;
}
}, "File size should not be larger than 100 kb");
$("#addForm").validate({
rules:{
userName:{
required: true,
minlength: 2,
maxlength: 10,
lettersonly:true
},
email:{
required: true,
email:true
},
homePage:{
required:true,
url: true
},
text:{
required:true,
lettersonly:true
},
captcha:{
required:true,
remote: {
url:"check-captcha.php",
type: "post"
}
},
userFile:{
required: true,
// extension: "txt"
}
},
messages:{
userName:{
required: "This field is required",
minlength: "Name must be at least 2 characters",
maxlength: "Maximum number of characters - 10"
},
captcha: {
remote: " NO!"
}
}
});
当我添加到userFile 扩展名:“txt”时,我收到错误消息: TypeError:t.validator.methods [a]未定义
如何解决我的问题?没有扩展我的代码工作
答案 0 :(得分:0)
您必须包含文件“additional-methods.min.js”。