Article from:/qq_38215042/article/details/84675988
The specific code is as follows:
if(Conditional statements){ return false; }else{ return true; } if(Conditional statements){ return false; }else{ return true; } if(Conditional statements){ return false; }else{ return true; }
Is this the case you wrote? In fact, only the first one will be executed. The main reason is that you have added all the if else statements, and return is used to return the result. This method is actually not wrong and the logic is very smooth. However, if you think about it carefully, if the return is true when you submit, then your form form is equivalent to receiving a command that can be submitted, and it will submit the form, and the subsequent if else statement will not be executed.
Solution: I use a kind of mark, give them true first. If the condition of the subsequent if statement is judged to be false, a pop-up box will pop up to prompt. Only when all of them meet the conditions will the form form be returned to true and form submission will be performed.
If you have a good method, please comment~~~~~~~~~~
1: js code
Need to introduce jquery
<script type="text/javascript"> function checkForm() { var productName = true; var chanDi = true; var muChanLiang = true; var zongChanLiang = true; var xiangQiang = true; var yuShouJia = true; var phone = true; var pattern = /^1[34578]\d{9}$/; // Verify the mobile phone number regular expression if ($("#userName") == "") { alert("Please enter the product name!"); /* alert($("#gsp_add_img_23").val()); */ productName = false; } else if ($("#candi").val() == "") { alert("Please enter the origin!"); chanDi = false; } else if ($("#muchan").val() == "") { alert("Please enter the yield per mu!"); muChanLiang = false; }else if ($("#zongChan").val() == "") { alert("Please enter the total output!"); zongChanLiang = false; } else if ($("#xiangqing").val() == "") { alert("Please enter product details!"); xiangQiang = false; } else if ($("#yushoujia").val() == "") { alert("Please enter the pre-sale price!"); yuShouJia = false; } else if (!($("#userPhone").val())) { alert("Mobile phone number format"); phone = false; } if (productName == true && chanDi == true && muChanLiang == true && zongChanLiang == true && xiangQiang == true && yuShouJia == true && phone == true) { $("#ListForm").submit(); } } </script>
2: form form
<form action="$!webPath/new/fl_npsh.htm" method="post" name="ListForm" enctype="multipart/form-data" > <!-- Similar to home page floor module --> <div style="width: 1020px; margin: 0 auto;"> <div class="floor floor_purple" style="width: 1011px; border-top: 1px solid #eee;"> <div class="liebiao" <!-- Pass one to the backgroundid --> <input name="id" type="hidden" value="$!" /> </div> <div class="liebiao"> <!-- <span class="liebiao_left" style="color:red">*</span> --> <span class="liebiao_left">Product Name:</span> <input type="text" name="product_name" class="shuruk" placeholder="example:LingbaoSODapple(*Required *)" /> </div> <div class="liebiao"> <span class="liebiao_left">Product origin:</span> <input type="text" value="" name="product_origin" class="shuruk" placeholder="example:Lingbao寺河山(*Required *)" /> </div> <div class="liebiao"> <span class="liebiao_left">Product yield per mu:</span> <input type="text" value="" name="mu_yield" class="shuruk" placeholder="example:Yield per mu(*Required *)" /> </div> <div class="liebiao"> <span class="liebiao_left">Total product output:</span> <input type="text" value="" name="sum_yield" class="shuruk" placeholder="example:Total output(*Required *)" id = 'zongChan'/> </div> <div class="liebiao" style="height: 60px;"> <span class="liebiao_left" style="display: inline-block; height: 60px; line-height: 30px; float: left;">Product details:</span> <textarea type="text" value="" name="product_details" class="shuruk02" placeholder="example:Product planting standards、Product Advantages、Product Performance、Storage precautions, etc.(*Required *)"></textarea> </div> <div class="liebiao"> <span class="liebiao_left">Product pre-sale reference price:</span> <input type="text" value="" name="advance_price" class="shuruk" placeholder="example:With each500gFor units(*Required *)" /> </div> <div class="liebiao"> <span class="liebiao_left">Contact information of site owner:</span> <input value="" name="phone" class="shuruk" placeholder="(*Required *)" /> </div> <div class="liebiao" style="margin-top: 30px;"> <input type="button" value="Apply now" class="tijiao" onclick="checkForm()"> </div> </div> </div> </form>
Summarize
The above is the problem that the editor introduced to you that only the first IF works in JS form verification. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!