简体   繁体   English

使用javascript验证表单字段是否为空或为空

[英]validate form field for null or empty using javascript

I am using the following javascript code to check for nothing being entered in a form and it is not working. 我正在使用以下javascript代码来检查是否没有在表单中输入任何内容,并且它不起作用。 Can anyone suggest a reason? 有人可以提出原因吗?

           function validateForm()
{
    var a=document.getElementById("quiz_01").value;
    $question = a;
    if (a=="" || a==null) {
      alert("Question 201 must be filled out");
      form1.quiz_01.focus();
      return false;
      }
}

An extract of the html is as follows: html的摘录如下:

<form id="form1" name="form1" method="post" action=""  onsubmit="return validateForm()">
<table class="table">
    <tr>
        <td>
            <label for="quiz_01">201 A one followed by 100 zeros is a Googol</label>
        </td>
        <td>
            <select name="quiz_01" id="quiz_01">
                <option value=" "> </option>
                <option value="100">100</option>
                <option value="90">90</option>
                <option value="80">80</option>
                <option value="70">70</option>
                <option value="60">60</option>
                <option value="50">50</option>
                <option value="40">40</option>
                <option value="30">30</option>
                <option value="20">20</option>
                <option value="10">10</option>
                <option value="0">0</option>
            </select>
        </td>
    </tr>
</table>
<p>
    <input type="submit" name="next" value="Next &gt;">
</p>

The first option: 第一种选择:

<option value=" "> </option>

Has a value that is a single space, not an empty string. 其值是一个空格,而不是一个空字符串。 So either change the value or change the JS code. 因此,请更改value或更改JS代码。 Note also that the .value property will never be null so you don't need to test for that. 还要注意, .value属性永远不会为null因此您无需为此进行测试。

if (a == " ") {

Demo: http://jsfiddle.net/RyN5W/ 演示: http//jsfiddle.net/RyN5W/

修剪空白。

 var a=document.getElementById("quiz_01").value.trim();

change 更改

if (a=="" || a==null)

to

if (a==" " || a==null)

您应该按照HTML检查“”

if (a == " ") {

change the code to 将代码更改为

var e = document.getElementById("quiz_01");
    var ev = e.options[e.selectedIndex].text;

if(ev===' '){
alert('question 201 is a required field');

}

you should use === not == whwn comparing strings 您应该使用===而不是==比较字符串

You could not access the value directly when working with an <select> element. 使用<select>元素时,无法直接访问该值。 use following: 使用以下内容:

var element = document.getElementById("quiz_01");
var a = element.options[element.selecedIndex];

EDIT: (credits to nnnnnn ) 编辑:(积分为nnnnnn
In modern browsers the direct access will work, but if you're targeting an old Version (I think especially of Internet Explorer ;) ) the showed approach will gain you compatibility. 在现代浏览器中,可以直接访问,但是如果您以旧版本(我认为尤其是Internet Explorer;)为目标,则所显示的方法将获得兼容性。

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

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