html - validate form field for null or empty using javascript - Stack Overflow

I am using the following javascript code to check for nothing being entered in a form and it is not wor

I am using the following javascript code to check for nothing being entered in a form and it is not working. 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:

<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>

I am using the following javascript code to check for nothing being entered in a form and it is not working. 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:

<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>

Share Improve this question asked Jul 1, 2013 at 11:13 GeoffGeoff 1972 silver badges13 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 2

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. Note also that the .value property will never be null so you don't need to test for that.

if (a == " ") {

Demo: http://jsfiddle/RyN5W/

Trim the whitespaces.

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

change

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

to

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

You should be checking for " " as per your 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 paring strings

You could not access the value directly when working with an <select> element. use following:

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

EDIT: (credits to 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 patibility.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745408275a4626412.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信