本邮件内容由第三方提供,如果您不想继续收到该邮件,可 点此退订 。
jquery.validate验证text,checkbox,radio,selected 阅读原文»
jquery.validate是jquery的一个插件,用来辅助开发者在客户端方便快捷的实现表单验证,最终达到提高用户体验的目的。
官网:
http://plugins.jquery.com/validate/
示例代码
index.cshtml
< form id = "formLogin" method = "post" > < label for = "username" >Username:</ label > < input type = "text" id = "username" name = "username" /> < label for = "password" >password:</ label > < input type = "text" id = "password" name = "password" /> < label for = "password1" >password1:</ label > < input type = "text" id = "password1" name = "password1" /> < label for = "sex" >sex:</ label > < select id = "sex" name = "sex" > < option id = "sexopt" value = "" selected = "selected" >请选择</ option > < option id = "sexnan" value = "1" >男</ option > < option id = "sexnv" value = "2" >女</ option > < label for = "favorite" >level:</ label > < input type = "checkbox" id = "sport" name = "favorite" value = "sport" />sport < input type = "checkbox" id = "write" name = "favorite" value = "write" />write < label for = "level" >level:</ label > < input type = "radio" id = "one" name = "level" value = "one" />1 < input type = "radio" id = "two" name = "level" value = "two" />2 < input id = "submit" type = "button" value = "submit" /> @section scripts{ < script type = "text/javascript" src = "/content/home/index.js" ></ script > |
jquery.validate在定位html标签的时候,默认使用name属性来获取标签,所以需要开发者给需要进行验证的标签都加上name属性,并且赋值。
index.js
$().ready( function () { $( "#formLogin" ).validate({ required: "please input username" required: "please input password" required: "please select sex" required: "level requred" required: "favorite required" errorPlacement: function (error, element) { //指定错误信息位置 if (element.is( ':radio' ) element.is( ':checkbox' )) { //如果是radio或checkbox var eid = element.attr( 'name' ); //获取元素的name属性 error.appendTo(element.parent()); //将错误信息添加当前元素的父结点后面 error.insertAfter(element); $( "#submit" ).click( function () {
订阅:
博文评论 (Atom)
|
没有评论:
发表评论