【问题标题】:Form Validation- html and javascript表单验证 - html 和 javascript
【发布时间】:2018-09-05 00:01:48
【问题描述】:

我对编码比较陌生——处理表单验证,并且无法使用我的 formValidation 函数让页面实际返回 false(如果用户输入了不正确的 isbn 长度,则不提交表单)。想知道我在这里缺少什么。

无论 isbn.length 是什么,都会弹出警报并提交表单——但是,如果长度正确,它会被添加到数据库中并重新路由到主页。如果它不是正确的长度,它被路由到 “此页面无法正常工作 localhost 没有发送任何数据。 ERR_EMPTY_RESPONSE"


Javascript:

//function to validate the form's ISBN number input and ensure it's between 10-14 digits.

function formValidation(){
    var isbn = document.forms["sellForm"]["isbn"];

        if (isbn.length >= 10 && isbn.length <= 14){
         return true;

     }
     else
     {
         alert("Please input a 10-14 digit ISBN number");
         isbn.focus();
         return false;

     }
 }

</script>

对应的HTML:

<form name="sellForm" method="POST" action="/create" role="form" onsubmit="formValidation()">
              <div class="form-group">
                <label for="role" >ISBN</label>
                <input type="number" size=14 class="form-control" name="isbn" id="role"  required
               placeholder="input the 10-14 digit ISBN number"/>
              </div>

              <div class="form-group">
                  <label for="age">Condition</label>
                                    <br>
   <input type="radio" name="book_condition" value="Very Used"> Very Used<br>
     <input type="radio" name="book_condition" value="Lightly Used"> Lightly Used<br>
     <input type="radio" name="book_condition" value="Like New"> Like New
                  </div>
              </div>

              <div class="form-group">
                <label>Price</label>
                <input type="number" class="form-control" name="price" required
                                placeholder="input whole dollar price">
              </div>

              <button type="submit" class="btn btn-primary btn-md" id="add-btn">
                                <span class="fa fa-fire"></span> Sell</button>

            </form>

【问题讨论】:

  • 您收到警报了吗?任何控制台错误(按 F12 以调出浏览器工具,然后转到“控制台”选项卡)?
  • “无法让页面实际返回false”是什么意思?
  • console.log(isbn) 在您设置该变量后立即放置时会告诉您什么?定义了吗?另外,“10-12 位”还是“10-14”?警报消息说一个,代码和占位符文本说另一个。
  • 应该是 10-14。检查 console.log(isbn)
  • 一个。您应该将var isbn = document.forms["sellForm"]["isbn"]; 更改为var isbn = document.forms["sellForm"]["isbn"].value; b。并且,您应该调试 isbn 值。在网络浏览器中,按 f12 转到“控制台选项卡”,输入 console.log(isbn)。 C。 onsubmit="return formValidation()".

标签: javascript html forms validation


【解决方案1】:

您可能需要像这样检查 isbn.value:

function formValidation(){
  var isbn = document.forms["sellForm"]["isbn"];

  if (isbn.value >= 10 && isbn.value <= 14) {
       return true;
  } else {
       alert("Please input a 10-12 digit ISBN number");
       isbn.focus();
       return false;
  }
}

然后根据需要添加逻辑。

【讨论】:

  • 我认为他不是在检查值,而是检查长度,如果长度在 10 到 14 之间。
  • html 控件是一个数值控件,基于 isbn 编号,我认为不是要检查的长度。
  • 它对我有用,您能否详细说明您要完成的工作,以便向您发送完整的示例。
  • @AndreGamboa 您的回答似乎不正确。您必须像这样检查值的长度:isbn.value.length。查看我的答案,它包含一个可运行的 sn-p。
  • 我明白你在说什么,你说得对,应该是isbn.value.length
【解决方案2】:

您需要检查isbn 输入字段值的长度,并在表单标记中获取formValidation 函数在onsubmit 处理程序中的返回值,如下所示:onsubmit="return formValidation()"。

function formValidation(){
    var isbn = document.forms["sellForm"]["isbn"];
        // check for the input field value's length
        if (isbn.value.length >= 10 && isbn.value.length <= 14){
         return true;

     }
     else
     {
         alert("Please input a 10-12 digit ISBN number");
         isbn.focus();
         return false;

     }
 }
<form name="sellForm" method="POST" action="/create" role="form" onsubmit="return formValidation()">
              <div class="form-group">
                <label for="role" >ISBN</label>
                <input type="number" size=14 class="form-control" name="isbn" id="role"  required
               placeholder="input the 10-14 digit ISBN number"/>
              </div>

              <div class="form-group">
                  <label for="age">Condition</label>
                                    <br>
   <input type="radio" name="book_condition" value="Very Used"> Very Used<br>
     <input type="radio" name="book_condition" value="Lightly Used"> Lightly Used<br>
     <input type="radio" name="book_condition" value="Like New"> Like New
                  </div>
              </div>

              <div class="form-group">
                <label>Price</label>
                <input type="number" class="form-control" name="price" required
                                placeholder="input whole dollar price">
              </div>

              <button type="submit" class="btn btn-primary btn-md" id="add-btn">
                                <span class="fa fa-fire"></span> Sell</button>

            </form>

【讨论】:

  • 我也是这样。
  • @WhitehorseJJ 什么?没听懂?!
  • 别担心。这并不意味着。这个。
  • @WhitehorseJJ 你想说什么?
  • 没什么。你又快又聪明。
【解决方案3】:

您还有其他问题(请参阅Andre's answer),但您需要将return 添加为onsubmit 属性的一部分

<form name="sellForm" method="POST" action="/create" role="form" onsubmit="return formValidation();">

完整版

function formValidation() {  
  /*This is the form element*/ 
  var isbnEl = document.forms["sellForm"]["isbn"];
  var isbn = isbnEl.value;
  
  /*The following 2 lines are for demonstration only and can be removed*/
  console.log(isbnEl);
  console.log(isbn);
  
  if (isbn.length >= 10 && isbn.length <= 14) {
    return true;

  } else {
    alert("Please input a 10-12 digit ISBN number");
    isbnEl.focus();
    return false;
  }
}
<form name="sellForm" method="POST" action="/create" role="form" onsubmit="return formValidation()">
  <div class="form-group">
    <label for="role">ISBN</label>
    <input type="number" size=14 class="form-control" name="isbn" id="role" required placeholder="input the 10-14 digit ISBN number" />
  </div>

  <div class="form-group">
    <label for="age">Condition</label>
    <br>
    <input type="radio" name="book_condition" value="Very Used"> Very Used<br>
    <input type="radio" name="book_condition" value="Lightly Used"> Lightly Used<br>
    <input type="radio" name="book_condition" value="Like New"> Like New
  </div>
  <!--</div> <---- This is an orphan tag, remove it -->

  <div class="form-group">
    <label>Price</label>
    <input type="number" class="form-control" name="price" required placeholder="input whole dollar price">
  </div>

  <button type="submit" class="btn btn-primary btn-md" id="add-btn">
                                <span class="fa fa-fire"></span> Sell</button>

</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 2015-09-02
    相关资源
    最近更新 更多