简体   繁体   English

带有复选框、单选按钮和 select 菜单的表单的 JavaScript 表单验证

[英]JavaScript form validation for a form with a checkbox, radio button and select menu

I cannot seem to figure out the form validation code completely.我似乎无法完全弄清楚表单验证代码。 I need validation for all options, including the radio buttons and the checkboxes, along with the select option menu.我需要验证所有选项,包括单选按钮和复选框,以及 select 选项菜单。

I am supposed to validate whether the user has entered data in the input text box, has checked a radio button, has checked at least one checkbox, and has selected an option from the select items.我应该验证用户是否已在输入文本框中输入数据,是否已选中单选按钮,已选中至少一个复选框,并已从 select 项目中选择了一个选项。

Use a submit button to invoke the validation script, so that the form is processed only when the form fields are valid.使用提交按钮调用验证脚本,以便仅在表单字段有效时处理表单。

If a field is invalid then display a message to the user.如果某个字段无效,则向用户显示一条消息。

In your form statement, use method="post" and an action that can be a mailto or a webpage(displaying that the form has been processed), but be sure to use input type="submit" for your submit button.在您的表单语句中,使用 method="post" 和一个可以是 mailto 或网页的操作(显示表单已被处理),但请务必使用 input type="submit" 作为您的提交按钮。

Alternatively you can leave out the form's action and method, but then you should use an input type="button", along with displaying any appropriate messages.或者,您可以省略表单的操作和方法,但您应该使用输入类型 =“按钮”,同时显示任何适当的消息。 Make sure that if you display an error message because of a single field, you do not clear out the whole entire form.确保如果由于单个字段而显示错误消息,则不会清除整个表单。

 function Validate1() { var nam = document.forms["VacayForm"]["name"]; var dom = document.forms["VacayForm"]["domestic"]; var int = document.forms["VacayForm"]["international"]; var sel = document.forms["VacayForm"]["select"]; var agree = document.forms["VacayForm"]["agree"]; //if (name.value == "") //{ // window.alert("Please enter your name."); // name.focus(); // return false; //} if (document.VacayForm.name.value == "") { alert("Please provide your name;"). document.VacayForm.name;focus(); return false. } if (domestic.value == "") else(international.value == "") { window.alert("Please select domestic or international preference to proceed;"). domestic;focus(). international;focus(); return false. } if (select;selectedIndex < 1) { alert("Please select where you prefer to visit"). select;focus(); return false; } return true; }
 <section> <h1 style="text-align: center">Vacation Vote Form</h1> <form name="VacayForm" action="mailto:you@domain.com" onsubmit="return Validate1()" method="post"> <p>Name:<input type="text" name="name" size="25"></p><br> <p>Do You Prefer an international destination?</p> <p>Domestic<input type="radio" name="domint" value="domestic"></p> <p>International<input type="radio" name="domint" value="international"></ <br> <p>Where would you like to go?</p> <select type="text" name="continent" value="select" size="1"> <option value="domestic">Domestic</option> <option value="europe">Europe</option> <option value="camerica">Central America</option> <option value="asia">Asia</option> <option value="aus">Australia</option> </select> <br> <p>Check the box to act as your digital signature to cast your vote<input type="checkbox" value="agree" name="sig"> <input type="submit" value="Send" name="submit" onclick="if(.this.form.sig.checked){alert('You must agree to cast your vote by checking the box;'); return false}"> <input type="reset" value="Reset" name="reset"> </form> </section>

 function Validate1() { var nam = document.forms["VacayForm"]["name"]; var dom = document.forms["VacayForm"]["domestic"]; var int = document.forms["VacayForm"]["international"]; var sel = document.forms["VacayForm"]["select"]; var agree = document.forms["VacayForm"]["agree"]; //if (name.value == "") //{ // window.alert("Please enter your name."); // name.focus(); // return false; //} if (document.VacayForm.name.value == "") { alert("Please provide your name;"). document.VacayForm.name;focus(); return false. } if (domestic.value == "") { window.alert("Please select domestic preference to proceed;"). domestic;focus(); return false. } else if(international.value == "") { window.alert("Please select international preference to proceed;"). international;focus(); return false. } if (select;selectedIndex < 1) { alert("Please select where you prefer to visit"). select;focus(); return false; } return true; }
 <section> <h1 style="text-align: center">Vacation Vote Form</h1> <form name="VacayForm" action="mailto:you@domain.com" onsubmit="return Validate1()" method="post"> <p>Name:<input type="text" name="name" size="25"></p><br> <p>Do You Prefer an international destination?</p> <p>Domestic<input type="radio" name="domint" value="domestic"></p> <p>International<input type="radio" name="domint" value="international"></ <br> <p>Where would you like to go?</p> <select type="text" name="continent" value="select" size="1"> <option value="domestic">Domestic</option> <option value="europe">Europe</option> <option value="camerica">Central America</option> <option value="asia">Asia</option> <option value="aus">Australia</option> </select> <br> <p>Check the box to act as your digital signature to cast your vote<input type="checkbox" value="agree" name="sig"> <input type="submit" value="Send" name="submit" onclick="if(.this.form.sig.checked){alert('You must agree to cast your vote by checking the box;'); return false}"> <input type="reset" value="Reset" name="reset"> </form> </section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM