如何获取脚本以确保没有字段为空

很简单,我敢肯定...正如你可以看出的,这是相当新的!


我需要一个JavaScript代码来确保表单中没有字段为空,包括文本,数字,电子邮件,电话...如果有一个字段恰好为 null,则应提交警报。如果所有字段中都有一些数据,则应提交警报以确认一切正常。


任何人都可以帮忙吗?请?:D


我的代码:


<!DOCTYPE html>


<html>


<head>

  <title>Billma Tours - Bookings</title>

  <link rel="stylesheet" type="text/css" href="style.css">

  <script src="myscripts.js"></script>

</head>


<body>


  <h1> Billma Tours </h1>


  <ul id="menubar">

    <li><a href="index.html">Home</a></li>

    <li><a href="services.html">Services</a></li>

    <li><a href="faq.html">FAQ</a></li>

    <li><a href="customerbooking.html">Bookings</a></li>

  </ul>


  <br>


  <h2> Booking Management </h2>


  <div id="pagebrief">

    <p> Welcome to our booking page. If you wish to make a booking, please fill out the form below and submit your request. </p>

    <p> Please ensure that all fields are filled in. For reference, the information table is below. </p>

  </div>


  <br>


  <table>

    <tr id="tabletop">

      <td> Name of Tour Package </td>

      <td> Locations Included</td>

      <td> Cost Per Person </td>

      <td> Tour Guide </td>

      <td> Next Available Date </td>

    </tr>

    <tr>

      <td> Dine and Dusted </td>

      <td> Restaurants/Food retailers on central Wellington streets </td>

      <td> NZ$ 60.00 </td>

      <td> James Sanderson </td>

      <td> Monday, June 8, 2020 </td>

    </tr>

    <tr>

      <td> A View to a Thrill</td>

      <td> Wellington Cable Car, Brooklyn Wind Turbine, Mount Victoria </td>

      <td> NZ$ 130.50 </td>

      <td> Bill Colenso </td>

      <td> Thursday, June 11, 2020 </td>

    </tr>

    <tr>

      <td> Movie-ing On </td>

      <td> Weta Workshop, Park Road Post Production </td>

      <td> NZ$ 117.75 </td>

      <td> Xiulan Ma </td>

      <td> Wednesday, June 10, 2020 </td>

    </tr>

    <tr>

      <td> Water Sight to Behold </td>

      <td> Te Papa, New Zealand Parliament, Botanical Gardens </td>

      <td> NZ$ 150.00 </td>

      <td> James Sanderson </td>

      <td> Saturday, June 13, 2020 </td>

    </tr>

  </table>


  <br>


</html>


叮当猫咪
浏览 68回答 2
2回答

LEATH

在表单中,您需要将“选择”中的空白值设置为空<option value=""> Please select an option </option>并处理逻辑以检查无线电是否被检查为if (x[i].type == 'radio') {&nbsp; var checked = [...document.getElementsByName('Tour package')].find(c => c['checked']);&nbsp; if (checked == undefined) {&nbsp; &nbsp; &nbsp;valid = false;&nbsp; &nbsp; &nbsp;break;&nbsp; }}并使用 for 循环来检查窗体控件,如下所示&nbsp; function validateForm() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var x = document.forms["confirm"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var valid = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < x.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (x[i].type == 'radio') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var checked = [...document.getElementsByName('Tour package')].find(c => c['checked']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (checked == undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valid = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (x[i].value == null || x[i].value == '') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valid = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!valid) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Please fill in all available fields.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('Form is valid');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }<!DOCTYPE html><html><head>&nbsp; &nbsp; <title>Billma Tours - Bookings</title>&nbsp; &nbsp; <link rel="stylesheet" type="text/css" href="style.css">&nbsp; &nbsp; <script src="myscripts.js"></script></head><body>&nbsp; &nbsp; <h1> Billma Tours </h1>&nbsp; &nbsp; <ul id="menubar">&nbsp; &nbsp; &nbsp; &nbsp; <li><a href="index.html">Home</a></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><a href="services.html">Services</a></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><a href="faq.html">FAQ</a></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><a href="customerbooking.html">Bookings</a></li>&nbsp; &nbsp; </ul>&nbsp; &nbsp; <br>&nbsp; &nbsp; <h2> Booking Management </h2>&nbsp; &nbsp; <div id="pagebrief">&nbsp; &nbsp; &nbsp; &nbsp; <p> Welcome to our booking page. If you wish to make a booking, please fill out the form below and submit your&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request. </p>&nbsp; &nbsp; &nbsp; &nbsp; <p> Please ensure that all fields are filled in. For reference, the information table is below. </p>&nbsp; &nbsp; </div>&nbsp; &nbsp; <br>&nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; <tr id="tabletop">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Name of Tour Package </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Locations Included</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Cost Per Person </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Tour Guide </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Next Available Date </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Dine and Dusted </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Restaurants/Food retailers on central Wellington streets </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> NZ$ 60.00 </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> James Sanderson </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Monday, June 8, 2020 </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> A View to a Thrill</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Wellington Cable Car, Brooklyn Wind Turbine, Mount Victoria </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> NZ$ 130.50 </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Bill Colenso </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Thursday, June 11, 2020 </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Movie-ing On </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Weta Workshop, Park Road Post Production </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> NZ$ 117.75 </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Xiulan Ma </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Wednesday, June 10, 2020 </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Water Sight to Behold </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Te Papa, New Zealand Parliament, Botanical Gardens </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> NZ$ 150.00 </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> James Sanderson </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> Saturday, June 13, 2020 </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </table>&nbsp; &nbsp; <br>&nbsp; &nbsp; <div id="bookingform">&nbsp; &nbsp; &nbsp; &nbsp; <h4> Personal Details </h4>&nbsp; &nbsp; &nbsp; &nbsp; <p><span> First and Last Name: </span></p>&nbsp; &nbsp; &nbsp; &nbsp; <form name="confirm" onsubmit="return validateForm()" method="post">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><span> Phone Number: </span></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="tel" name="phone">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><span> Email Address: </span></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="email" name="emailaddress">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h4> Tour Information</h4>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><span> Tour package: </span></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="Tour package" value="Dine and Dusted"> Dine and Dusted <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="Tour package" value="A View to a Thrill"> A View to a Thrill <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="Tour package" value="Movie-ing On"> Movie-ing On <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="Tour package" value="Water Sight to Behold"> Water Sight to Behold <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><span> Amount of people attending: </span></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select name="no.pplattending">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value=""> Please select an option </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="1"> 1 </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="2"> 2 </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="3"> 3 </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="4"> 4 </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="5"> 5 </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="6"> 6 </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="7"> 7 </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="8"> 8 </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="9"> 9 </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="10+"> 10+ </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><span> Date of Tour: </span></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="date" name="tourdate"><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h4> Payment Details </h4>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><span> Name on Card: </span></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="cardname">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><span> Card Number: </span></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" name="cardnumber">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><span> Card Expiry: </span></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" name="cardexpmonth">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" name="cardexpyear">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><span> Card Security Code: </span></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" name="cardsecurity">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Submit">&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </script></body></html>

胡说叔叔

你必须像这样添加:return false;function validateForm() {&nbsp; &nbsp; var x = document.forms["confirm"]["code"].value;&nbsp; &nbsp; if (x == null) {&nbsp; &nbsp; &nbsp; &nbsp; alert("Please fill in all available fields.");&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}(并且您忘记了警报功能后面的分号)也许HTML5表单验证也会很有趣:https://www.w3schools.com/html/html_form_attributes.asp
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript