0

我有一个任务,我认为我几乎完成了,但我的头脑不会付出太多,也许我可以得到帮助;我的任务是创建一个收集用户数据并验证用户信息的 html 表单;我认为我做得对。现在我有这个我不理解的说明,但认为它是在验证过程之后“商品应该包含指向 amazon.com 或其他零售商网站上相同或相似商品的链接,以便潜在买家可以更好地了解新品的商品和价格;" 那么我必须在用户单击提交按钮后弹出一个竖起大拇指的图像,但我已经在 onclick 中有我的验证表单。

这是我的代码:

 <script language="JavaScript" type="text/javascript">

 //Function to validate form

 function validateForm() {

     //Variables are declared

     var s=document.forms["craigslistSalesForm"]["itemName"].value;
     var t=document.forms["craigslistSalesForm"]["itemPrice"].value;
     var u=document.forms["craigslistSalesForm"]["location"].value;
     var v=document.forms["craigslistSalesForm"]["itemCondition"].value;
     var w=document.forms["craigslistSalesForm"]["itemDescription"].value;

     //If itemName is entered continue but if left blank show alert
     if (s==null || s=="") {
     alert("Please enter a title for your posting");
     return false;
     }

     //If itemPrice is entered continue but if left blank show alert
     if (t==null || t=="") {
     alert("Please enter the price of your item");
     return false;
     }

     //If location is entered continue but if left blank show alert
     if (u==null || u=="") {
     alert("Please enter your location");
     return false;
     }

     //If itemCondition is entered continue but if left blank show alert
     if (v==null || v=="Select a condition option") {
     alert("Please choose the condition of your item from the list");
     return false;
     }

     //Variables are declared

     var x=document.forms["craigslistSalesForm"]["email"].value;
     var atpos=x.indexOf("@");
     var dotpos=x.lastIndexOf(".");
     var y=document.forms["craigslistSalesForm"]["email2"].value;

     //If email address doesn't contain symbol @ or . and a minimum 2 characters
     //after dot then alert to correct email address
     if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
     alert("Your email address must be in the format of name@domain.com");
     return false;
     }

     //If second email address does not match first email then alert mis-match
     if (y!=x) {
     alert("Ooops! Your email does not match, please try again.");
     return false;
     }

     //If itemDescription is entered continue but if left blank show alert
     if (w==null || w=="") {
     alert("You must describe your Item for Sale");
     return false;
     }

     }

 </script>

 <html>
 <head>
 <title>Project Task #1 - AGV</title>
 </head>
 <body onload="document.getElementById('itemName').focus()" bgcolor="E1E1E1">
 <form id="craigslistSalesForm">
 <h2>PA Craigslist - Items for sale</h2>
 <div>
 <span>Posting Title:&#160; </span><input tabindex="1" type="text" name="itemName"
 id="itemName" size="35" maxlength="64" value>
 &#160; &#160; <span>Price:&#160; </span>$<input type="text" name="itemPrice"
 id="itemPrice" size="6" maxlength="9" value>
 </div>
 <br>
 <div>
 <span>Location:&#160; &#160; </span><input type="text" name="location"
 id="location" size="35" maxlength="64" value>
 &#160; &#160; &#160; <span>Item condition:&#160; </span>
                 <select id="itemCondition" value>
                 <option>Select a condition option</option>
                 <option>New</option>
                 <option>Like new (in original packaging)</option>
                 <option>Like new (without original packaging
 </option>
                 <option>Fairly used</option>
                 <option>Used</option>
                 <option>Rough condition</option>
                 <option>Working</option>
                 <option>Not working</option>
                 <option>I don't know</option>
                 </select>
 </div>
 <div>
 &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;
 &#160;
 <input type="radio" value="Residential" name="typeOfLocation" CHECKED>Residential
 &#160; &#160; <input type="radio" value="Business" name="typeOfLocation">Business
 </div>
 <br>
 <div>
 <span>Your email:&#160; </span><input type="text" name="email" id="email"
 size="35" maxlength="64" value>
 &#160; &#160; <span><i>Retype your email:&#160; </i></span><input type="text"
 name="email2" id="email2" size="35" maxlength="64" value>
 </div>
 <br>
 <div>
 <span><b>Item Description:</b></span>
 <br>
 <textarea cols="80" name="itemDescription" id="itemDescription" style="width: 70%;
 border: 3px solid black; font-family: Calibri; font-size: 24px;" rows="10"></textarea>
 </div>
 <button type="button" value="callTwoFunctions" onclick="validateForm()">
 <b>Submit</b></button>
 </form>
 </body>
 </html>
4

1 回答 1

0

在您的脚本中,您可以在您执行的所有验证的下方添加其他条目,这将被视为成功的验证结果:

function validateForm() {

 //Variables are declared

 var s=document.forms["craigslistSalesForm"]["itemName"].value;
 var t=document.forms["craigslistSalesForm"]["itemPrice"].value;
 var u=document.forms["craigslistSalesForm"]["location"].value;
 var v=document.forms["craigslistSalesForm"]["itemCondition"].value;
 var w=document.forms["craigslistSalesForm"]["itemDescription"].value;

 //If itemName is entered continue but if left blank show alert
 if (s==null || s=="") {
 alert("Please enter a title for your posting");
 return false;
 }

 //If itemPrice is entered continue but if left blank show alert
 if (t==null || t=="") {
 alert("Please enter the price of your item");
 return false;
 }

 //If location is entered continue but if left blank show alert
 if (u==null || u=="") {
 alert("Please enter your location");
 return false;
 }

 //If itemCondition is entered continue but if left blank show alert
 if (v==null || v=="Select a condition option") {
 alert("Please choose the condition of your item from the list");
 return false;
 }

 //Variables are declared

 var x=document.forms["craigslistSalesForm"]["email"].value;
 var atpos=x.indexOf("@");
 var dotpos=x.lastIndexOf(".");
 var y=document.forms["craigslistSalesForm"]["email2"].value;

 //If email address doesn't contain symbol @ or . and a minimum 2 characters
 //after dot then alert to correct email address
 if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
 alert("Your email address must be in the format of name@domain.com");
 return false;
 }

 //If second email address does not match first email then alert mis-match
 if (y!=x) {
 alert("Ooops! Your email does not match, please try again.");
 return false;
 }

 //If itemDescription is entered continue but if left blank show alert
 if (w==null || w=="") {
 alert("You must describe your Item for Sale");
 return false;
 }


 // A place to do additional thing after successful validation
 // you can have another function call
 // i.e. popupImageContainer();

 alert("Yahoo validation is successful lets put another function calls instead this alert code");

 }

...您还可以通过在按钮标记中的事件中附加其调用后validateForm()的函数来让您的其他函数弹出一些东西:onclick

`<button type="button" value="callTwoFunctions" onclick="validateForm();popupDialog();">`
于 2012-09-20T15:25:16.810 回答