0

I'm having trouble with validating my dropdown list with javascript. What I want is to display an error message underneath the field inside span tags if the user hasn't selected any options. I've checked almost every tutorials out there but no luck.

Here is the code for the form:

<form name="subform" id="subform" action="submit.php" onsubmit="return checkForBlank()" method="POST" enctype="multipart/form-data">

    <div class="md-form">
        <input type="text" id="subcim" name="subcim" class="form-control"><span class="error_form" id="subcim_error_message"></span>
        <label for="subcim" class="">Title</label><br>
    </div>

<div class="md-form">
    <select class="mdb-select" id="subcat" name="subcat"><span class="error_form" id="subcat_error_message"></span>
        <option value="0" selected>Please select an option</option>
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
    </select>
    <label id="subcat_info">Category</label><br>
    </div>

<button type="submit" class="btn btn-primary btn-lg btn-block">Submit</button>
</form>

and here is the Javascript code (the first if statement validates the input field in the form:

function checkForBlank() {
if(document.getElementById("subcim").value == "") {
    document.getElementById("subcim_error_message").textContent="You must add a title!";
    return false;
} else if(document.getElementById("subcim").value.length < 5 || document.getElementById("subcim").value.length > 80) {
    document.getElementById("subcim_error_message").textContent="The title must be between 5 and 80 characters!";
    return false;
}

var result = document.getElementById('subcat').value;
if (result === "0") {
    document.getElementById("subcat_error_message").textContent="You must select an option!";
    return false;
}
}

I'm using

 onsubmit="return checkForBlank()"

inside the form tag.

When I submit the form without selecting an option, the form seems to be submitted properly, but it does not display the error message.

Any help is appreciated

4
  • Try putting an blank alert to check weather your function is being called or not? And also if possible change textContent to innerHTML. And please Provide whole code for form like where you put submit button and also how you put onsubmit event also. Commented Dec 8, 2016 at 9:59
  • Added the whole code. I checked it with only alert. If I replace this document.getElementById("subcat_error_message").textContent="You must select an option!"; with an alert, the alert works. The innerHTML doesn't have any effect. Commented Dec 8, 2016 at 10:23
  • 2
    I just came to realize that the place where you put your span is wrong. You can't put your span inside your select tag. It should be out side your select tag. Just do that much change and your code will just work fine. Commented Dec 8, 2016 at 10:44
  • Thanks, that was the problem Commented Dec 8, 2016 at 11:09

4 Answers 4

4
    function Validate() {
        var subcat= document.getElementById("subcat");
        if (subcat.value == "") {
            //If the "Please Select" option is selected display error.
            alert("Please select an option!");
            return false;
        }
        return true;
    }


    onsubmit ="return Validate()"
Sign up to request clarification or add additional context in comments.

1 Comment

Change your html code set "" or leave blank in first option value
1
  function Validate() {
        var subcat= document.getElementById("subcat");
        if (subcat.value == "0") {
            //If the "Please Select" option is selected display error.
            alert("Please select an option!");
            return false;
        }
        return true;
    }


    onsubmit ="return Validate();"

2 Comments

Thanks, but I don't want an alert message, I'd like to display a text message underneath the field.
yes you can display text message instead of alert. you just need to paste your text content there.
0

Change your syntex from

<div class="md-form">
    <select class="mdb-select" id="subcat" name="subcat"><span class="error_form" id="subcat_error_message"></span>
        <option value="0" selected>Please select an option</option>
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
    </select>
    <label id="subcat_info">Category</label><br>
</div>

To syntex as below

<div class="md-form">
    <select class="mdb-select" id="subcat" name="subcat">
        <option value="0" selected>Please select an option</option>
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
    </select>
    <span class="error_form" id="subcat_error_message"></span>
    <label id="subcat_info">Category</label><br>
</div>

The only mistake that you are doing is putting your error span within your select tag it self which is not a predefined HTML format for select tag. As you can see that after putting your span tag out side your select tag every thing will work as you want.

Comments

0

Html

<div class="md-form">
        <select class="mdb-select" id="subcat" name="subcat">
            <option value="0" selected>Please select an option</option>
            <option value="option1">Option 1</option>
            <option value="option2">Option 2</option>
        </select>
<span class="error_form" id="subcat_error_message"></span>
        <label id="subcat_info">Category</label><br>
        </div>

JavaScript

function Validate() {
                    var subcat= document.getElementById("subcat");
                    if (subcat.value == "0") {
                        //If the "Please Select" option is selected display error.
                    document.getElementById("subcat_error_message").innerHTML="Error Message";    
    //OR
               document.getElementById("subcat_error_message").textContent="Error Message"; 
        return false;
                    }
                    return true;
                }

                onsubmit ="return Validate();"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.