JQuery validate 插件 Remote 异步验证
发布时间:2018-11-13 编辑:小张个人博客 查看次数:7409
jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求。该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同时提供了一个用来编写用户自定义方法的 API。所有的捆绑方法默认使用英语作为错误信息,且已翻译成其他 37 种语言。
访问 jQuery Validate 官网,下载最新版的 jQuery Validate 插件。
<div class="form-group "> <label for="title" class="control-label col-lg-2">标题</label> <div class="col-lg-6"> <textarea name="title" class="form-control" rows="3" placeholder="标题" >{{ $question -> title}}</textarea> <input type="hidden" name="_token" value="{{csrf_token()}}"/> </div> </div>
<script> <script src="https://cdn.bootcss.com/jquery-validate/1.18.0/jquery.validate.min.js"></script> // jQuery Validate 表单验证 // 自定验证方法 // 以字母开头,5-17 字母,数字,下划线 jQuery.validator.addMethod("user", function(value, element) { var tel = /^[a-zA-Z][\w]{4,16}$/; return this.optional(element) || (tel.test(value)); }, "以字母开头,5-17 字母,数字,下划线"); $('form[name=Category]').validate({ // 错误提示信息放span标签里 errorElement: 'span', // 验证规则 rules:{ title: { required: true, remote: { url: "{{ url('validateTitle') }}", type: "post", dataType:"json", data: { title: function() {return $("textarea[name=title]").val();}, _token: "{{ csrf_token() }}" } } } }, // 设置提示信息 messages : { title: { required: '标题不能为空。', remote: '试题已存在。' } } }); </script>
后台查询验证( Laravel 5 )
// 验证标题是否已存在 public function validateTitle(Request $request) { $title = $request->input('title',''); $result = Question::where('title', $title)->orderBy('qid','desc')->first(); return $result ? "false" : "true"; }
JQuery validate 插件 Remote 异步验证,到此结束!
Copyright © 小张个人博客 All Rights Reserved 渝ICP备15006773号-1
联系方式:[email protected] | 本站文章仅供学习和参考