【问题标题】:Form validation submission and First letter capitalized in output of submission of Name and City表单验证提交和姓名和城市提交输出中的首字母大写
【发布时间】:2015-08-13 01:29:18
【问题描述】:

您好,这是我的表单验证提交的最后一次求助。当我验证失败时,表单仍然提交。如果我什至没有通过一次验证,我需要知道如何阻止表单提交。

如果可能,我需要在提交时按以下格式打印“名称”和“城市”字段。

                           Name: Namestring
                           City: Citystring

在“Name”和“City”的字符串中,必须以大写字母开头,后跟所有小写字母。

例如,如果用户键入“peTer. ”在名称框中,“Philadelphia”在城市框中,输出消息应显示为:彼得或城市。第一个字母必须始终是大写 via 提交时的输出。基本上无论我如何在输出中输入名称和城市字段,第一个字母都是大写字母,其余字母是小写字母。

function doClear()
{
    document.PizzaForm.customer.value ="";
    document.PizzaForm.address.value = "";
    document.PizzaForm.city.value = "";
    document.PizzaForm.state.value="PA";
    document.PizzaForm.zip.value="";
    document.PizzaForm.phone.value="";
    document.PizzaForm.email.value="";


    document.PizzaForm.sizes[0].checked = false;
    document.PizzaForm.sizes[1].checked = false;
    document.PizzaForm.sizes[2].checked = false;
    document.PizzaForm.sizes[3].checked = false;

    document.PizzaForm.toppings[0].checked = false;
    document.PizzaForm.toppings[1].checked = false;
    document.PizzaForm.toppings[2].checked = false;
    document.PizzaForm.toppings[3].checked = false;
    document.PizzaForm.toppings[4].checked = false;
    document.PizzaForm.toppings[5].checked = false;
    document.PizzaForm.toppings[6].checked = false;
    document.PizzaForm.toppings[7].checked = false;
    document.PizzaForm.toppings[8].checked = false;

    return;
}

function validateMe() {

   if ( !(validateText() && validateRadio() && validateTops()) ) {
      return false;
   }

    var toppings = ""
    for(i=0;i<document.PizzaForm.toppings.length;i++){
        if(document.PizzaForm.toppings[i].checked)
            toppings += (i==0?"":",")+document.PizzaForm.toppings[i].value;
    }

    alert("Name:" + " " + document.PizzaForm.customer.value + 
          '\n' + 
          "Address:" + " " + document.PizzaForm.address.value + 
          '\n' +
          "City:" + " " + document.PizzaForm.city.value +
          '\n' +
          "State:" + " " + document.PizzaForm.state.value +
          '\n' +
          "Zip:" + " " + document.PizzaForm.zip.value +
          '\n' +
          "Phone:" + " " + document.PizzaForm.phone.value +
          '\n' +
          "Email:" + " " + document.PizzaForm.email.value +
          '\n' +
          "Size:" + " " + document.PizzaForm.sizes.value +
          '\n' +
          "Toppings:" + " " + toppings);
    return;
}
function validateText()
{
    var customer = document.PizzaForm.customer.value;
    if (customer.length == 0)
    {
        alert("Please enter your name.")
    }
    var address = document.PizzaForm.address.value;
    if (address.length == 0)
    {
        alert("Please enter your address.")
    }
    var city = document.PizzaForm.city.value;
    if (city.length == 0)
    {
        alert("Please enter your city.")
    }
    var state = document.PizzaForm.state.value;
    if (state.length == 0)
    {
        alert("Please enter your state.")
    }
    var zip = document.PizzaForm.zip.value;
    if (document.PizzaForm.zip.value == "" ||
    isNaN( document.PizzaForm.zip.value ) ||
    document.PizzaForm.zip.value.length != 5 )
    {
        alert("Please enter the required zip code format.")
    }

    var phone = document.PizzaForm.phone.value;
    if (!/^\d{3}-\d{3}-\d{4}$/.test(document.PizzaForm.phone.value)) {
    alert("Please enter the correct phone format xxx-xxx-xxxxx.");

    }

    var email = document.PizzaForm.email.value;
        atpos = email.indexOf("@");
        dotpos = email.lastIndexOf(".");
    if (atpos < 1 || ( dotpos - atpos < 2 ))
    {
        alert("Please enter a valid email address.")
        return false;
    }
        return true;
}


function validateRadio()
{
    if (document.PizzaForm.sizes[0].checked) return true;
    if (document.PizzaForm.sizes[1].checked) return true;
    if (document.PizzaForm.sizes[2].checked) return true;
    if (document.PizzaForm.sizes[3].checked) return true;
    if (document.PizzaForm.sizes.value == false);
    {
        alert("Please select a pizza size.");
    }
    return;
}

function validateTops()
{
    if (document.PizzaForm.toppings[0].checked == false &&
        document.PizzaForm.toppings[1].checked == false &&
        document.PizzaForm.toppings[2].checked == false &&
        document.PizzaForm.toppings[3].checked == false &&
        document.PizzaForm.toppings[4].checked == false &&
        document.PizzaForm.toppings[5].checked == false &&
        document.PizzaForm.toppings[6].checked == false &&
        document.PizzaForm.toppings[7].checked == false &&
        document.PizzaForm.toppings[8].checked == false)
        {
            alert("Please select a topping of your choice.");
        }
            return false;
            return true;
}


</script> 
</head>
<body>

<form name="PizzaForm"   onsubmit="return validateMe()">
<h1>The JavaScript Pizza Parlor</h1>
<p>
<h4>Step 1: Enter your Name, Address, City, State Phone number, and Email:</h4>
<font face ="Courier New">
Name: &nbsp;&nbsp;&nbsp;<Input name="customer" size="50" type="text"><br>
Address:&nbsp;<Input name="address" size="50" type="text"><br>
City:  &nbsp;&nbsp;&nbsp;<Input name="city" size="15"type="text">
State:<select name="state"> 
      <option value="">Select</option>
      <option value="PA">PA</option>
      <option value="NJ">NJ</option>
      <option value="NY">NY</option>
      <option value="DE">DE</option>
      </select>
Zip:<Input name="zip" size="7"type="text"><br>
Phone: &nbsp;&nbsp;<Input name="phone" size="50"type="text"><br>
Email: &nbsp;&nbsp;<Input name="email" size="31"type="text"><br>
</font>
</p>
<p>
<h4>Step 2: Select the size of pizza you want:</h4>
<font face="Courier New">
<input name="sizes" type="radio" value="Small">Small
<input name="sizes" type="radio" value="Medium">Medium
<input name="sizes" type="radio" value="Large">Large
<input name="sizes" type="radio" value="Jumbo">Jumbo<br>
</font>
</p>
<p>
<h4>Step 3: Select the pizza toppings you want:</h4>
<font face="Courier New">
<input name="toppings" type="checkbox" value="Pepperoni">Pepperoni
<input name="toppings" type="checkbox" value="Canadian Bacon">Canadian Bacon
<input name="toppings" type="checkbox" value="Sausage">Sausage<br>
<input name="toppings" type="checkbox" value="Mushrooms">Mushrooms
<input name="toppings" type="checkbox" value="Pineapple">Pineapple
<input name="toppings" type="checkbox" value="Black Olives">Black Olives<br>
<input name="toppings" type="checkbox" value="Green Peppers">Green Peppers
<input name="toppings" type="checkbox" value="Extra Cheese">Extra Cheese
<input name="toppings" type="checkbox" value="Plain">Plain<br>
</font>
</p>
<!--
========================================================
The submit and clear form buttons below.
========================================================
-->
<input type="button" value="Submit Order" onClick="Submit Order">
<input type="button" value="Clear Entries" onClick="doClear()">

</form>
</body>

</html>

我需要能够满足我要求的最小代码。仅 Java 脚本而非 JQuery,因为我在该领域没有经验。

谢谢大家

【问题讨论】:

    标签: javascript forms validation form-submit


    【解决方案1】:

    利用表单的 onsubmit 属性。

    HTML:

    <form onsubmit="return validateMe()">
       <!-- whatever fields go here -->
    </form>
    

    Javascript:

    function validateMe() {
       // Return false if invalid input.
       if ( !(validateText() && validateRadio() && validateTops()) ) {
          return false;
       }
    
       // Otherwise, we're planning on submitting the form.
       // Alert a confirmation and return true.
    
       var toppings = "";
       for(i=0;i<document.PizzaForm.toppings.length;i++){
         if(document.PizzaForm.toppings[i].checked)
            toppings += (i==0?"":",")+document.PizzaForm.toppings[i].value;
       }
    
       alert("Name:" + " " + document.PizzaForm.customer.value + 
          '\n' + 
          "Address:" + " " + document.PizzaForm.address.value + 
          '\n' +
          "City:" + " " + document.PizzaForm.city.value +
          '\n' +
          "State:" + " " + document.PizzaForm.state.value +
          '\n' +
          "Zip:" + " " + document.PizzaForm.zip.value +
          '\n' +
          "Phone:" + " " + document.PizzaForm.phone.value +
          '\n' +
          "Email:" + " " + document.PizzaForm.email.value +
          '\n' +
          "Size:" + " " + document.PizzaForm.sizes.value +
          '\n' +
          "Toppings:" + " " + toppings);
    
       return true;
    }
    

    将您的“提交订单”按钮更改为:

    <input type="submit" value="Submit Order">
    

    您可以完全删除 doSubmit 函数。

    字符串首字母大写:

    function capitalizeString(stringToCapitalize) {
        var words = stringToCapitalize.split(' ');
        for (var i=0, il=words.length; i<il; i++) {
            if (words[i].length > 0) {
                words[i] = words[i].charAt(0).toUpperCase()
                           + words[i].substring(1, words[i].length);
            }
        }
        return words.join(' ');
    }
    

    就你的城市而言:

    alert("City: " + capitalizeString(document.PizzaForm.city.value));
    

    【讨论】:

    • 我在哪里放置和/或放置函数 validateMe()。我问是因为当我把它放在我的表格中时,它甚至没有提交。我尝试将它放在脚本的底部并在括号中输入表单的名称,但没有任何效果。
    • 您可以将它放在与您定义的其他函数一致的任何位置。如果表单输入有效,该函数需要返回 true,否则返回 false。我会更新答案。
    • 我想我的问题是我的编程经验。我真的不知道检查是否无效的逻辑。基本上是什么放在括号里。
    • 你已经开始这样做了。那些名为 validateText、validateRadio、validateTops 的函数都返回真或假,具体取决于是否给出了有效输入。关键是,无论你在 onsubmit 中使用什么函数,如果它返回 false,表单将不会提交。
    • 代码看起来不错,因为如果我什么都不选择或什么都不输入,我会收到警报,但是一旦我输入所有信息和选择,当我点击提交按钮时表单不会提交并且没有选择输出和完成的文本字段。这是什么原因造成的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    相关资源
    最近更新 更多