Verify whether the red submit button is displayed
bindBlur:function(){//jquery多级验证表单 var n = $('#item_name'); var p = $('#price'); var r = $('#reserve'); show(velidate());//页面加载之后先进行一次验证 //分别对三个需要验证的字段进行验证绑定,这里也可以写成一句代码 //$('#item_name','#price',$('#reserve')).on({blur:function(){show(veridate())}}); n.on({blur:function(){show(velidate())}}); p.on({blur:function(){show(velidate())}}); r.on({blur:function(){show(velidate())}}); function velidate(){//获取验证的结果 var flag = true; if(n.val()==""){flag= false;} if(p.val()=="0" || p.val()==""){flag= false;} if(r.val()=="0" || r.val()==""){flag= false;} return flag; } function show(flag){//将验证结果反映到页面 if(flag){$(".down-complete-btn").css("background-color","#b02125");} else{$(".down-complete-btn").css("background-color","#8f8f8f");} } },
Verification, called when the user clicks submit, will locate areas that need improvement
check:function(){ var n = $('#item_name'); var p = $('#price'); var r = $('#reserve'); if(n.val()==""){n.focus();return false;} if(p.val()=="0" || p.val()==""){p.focus();return false;} if(r.val()=="0" || r.val()==""){r.focus();return false;} return true; },
This section is for ajax submission and verification before submission
postData:function(){ $(".down-complete-btn").click(function(){ if(Add.check()){ $.ajax({ type : 'post', dataType : 'json', data : { id : $("#item_id").val(), name : $("#item_name").val(), price : $("#price").val(), photos : $("#photos").val(), }, cache : false, url : '/main/goods/add', success : function(data){ if(data.code==1){ alert("修改成功"); }else{ console.log(data); } }, error : function(){ alert('网络异常'); } }); } }); }
easy_form_validate.js
require.config({ paths:{ "jquery":"./lib/jquery-1.11.1.min", 'icon_Upload':'./icon_Upload' } }); require(['jquery','icon_Upload'],function(){ Add.bindBlur(); Add.postData(); }); var Add = { bindBlur:function(){//jquery多级验证表单 var n = $('#item_name'); var p = $('#price'); var r = $('#reserve'); show(velidate());//页面加载之后先进行一次验证 //分别对三个需要验证的字段进行验证绑定,这里也可以写成一句代码 //$('#item_name','#price',$('#reserve')).on({blur:function(){show(velidate())}}); n.on({blur:function(){show(velidate())}}); p.on({blur:function(){show(velidate())}}); r.on({blur:function(){show(velidate())}}); function velidate(){//获取验证的结果 var flag = true; if(n.val()==""){flag= false;} if(p.val()=="0" || p.val()==""){flag= false;} if(r.val()=="0" || r.val()==""){flag= false;} return flag; } function show(flag){//将验证结果反映到页面 if(flag){$(".down-complete-btn").css("background-color","#b02125");}else{$(".down-complete-btn").css("background-color","#8f8f8f");} } }, check:function(){ var n = $('#item_name'); var p = $('#price'); var r = $('#reserve'); if(n.val()==""){n.focus();return false;} if(p.val()=="0" || p.val()==""){p.focus();return false;} if(r.val()=="0" || r.val()==""){r.focus();return false;} return true; }, postData:function(){ $(".complete-btn").click(function(){ if(Add.check()){ $.ajax({ type : 'post', dataType : 'json', data : { id : $("#item_id").val(), name : $("#item_name").val(), summary : $("#summary").text(), price : $("#price").val(), store : $("#store").val(), mobileDetail : $("#detail").val(), photos : $("#photos").val(), brokerage : $("#brokerage").val(), flag : $("#flag").val(), }, cache : false, url : '/main/goods/add', success : function(data){ if(data.code==1){ alert("修改成功"); }else{ console.log(data); } }, error : function(){ alert('网络异常'); } }); } }); } };
Let’s look at another verification code
<script type="text/javascript"> //<![CDATA[ $(function(){ /* *思路大概是先为每一个required添加必填的标记,用each()方法来实现。 *在each()方法中先是创建一个元素。然后通过append()方法将创建的元素加入到父元素后面。 *这里面的this用的很精髓,每一次的this都对应着相应的input元素,然后获取相应的父元素。 *然后为input元素添加失去焦点事件。然后进行用户名、邮件的验证。 *这里用了一个判断is(),如果是用户名,做相应的处理,如果是邮件做相应的验证。 *在jQuery框架中,也可以适当的穿插一写原汁原味的javascript代码。比如验证用户名中就有this.value,和this.value.length。对内容进行判断。 *然后进行的是邮件的验证,貌似用到了正则表达式。 *然后为input元素添加keyup事件与focus事件。就是在keyup时也要做一下验证,调用blur事件就行了。用triggerHandler()触发器,触发相应的事件。 *最后提交表单时做统一验证 *做好整体与细节的处理 */ //如果是必填的,则加红星标识. $("form :input.required").each(function(){ var $required = $("<strong class='high'> *</strong>"); //创建元素 $(this).parent().append($required); //然后将它追加到文档中 }); //文本框失去焦点后 $('form :input').blur(function(){ var $parent = $(this).parent(); $parent.find(".formtips").remove(); //验证用户名 if( $(this).is('#username') ){ if( this.value=="" || this.value.length < 6 ){ var errorMsg = '请输入至少6位的用户名.'; $parent.append('<span class="formtips onError">'+errorMsg+'</span>'); }else{ var okMsg = '输入正确.'; $parent.append('<span class="formtips onSuccess">'+okMsg+'</span>'); } } //验证邮件 if( $(this).is('#email') ){ if( this.value=="" || ( this.value!="" && !/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value) ) ){ var errorMsg = '请输入正确的E-Mail地址.'; $parent.append('<span class="formtips onError">'+errorMsg+'</span>'); }else{ var okMsg = '输入正确.'; $parent.append('<span class="formtips onSuccess">'+okMsg+'</span>'); } } }).keyup(function(){ $(this).triggerHandler("blur"); }).focus(function(){ $(this).triggerHandler("blur"); });//end blur //提交,最终验证。 $('#send').click(function(){ $("form :input.required").trigger('blur'); var numError = $('form .onError').length; if(numError){ return false; } alert("注册成功,密码已发到你的邮箱,请查收."); }); //重置 $('#res').click(function(){ $(".formtips").remove(); }); }) //]]> </script>

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Enhance Your Code Presentation: 10 Syntax Highlighters for Developers Sharing code snippets on your website or blog is a common practice for developers. Choosing the right syntax highlighter can significantly improve readability and visual appeal. T

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

This article presents a curated selection of over 10 tutorials on JavaScript and jQuery Model-View-Controller (MVC) frameworks, perfect for boosting your web development skills in the new year. These tutorials cover a range of topics, from foundatio

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
