Prateek Mathur
Prateek Mathur

Reputation: 333

Checking for empty fields on HTML form

I am trying to create a form in HTML, and also trying to ensure that any field is not left empty.

Here is the HTML for the form

<form role="form" id="companyDetails" name="companyDetails" method="post" action="saveCompanyDetails.jsp" onsubmit="return false;">
    <div class="col-lg-6">
        <div class="form-group">
            <label>Company Name</label>
            <input type="text" class="form-control" id="cmpname" name="cmpname">
            <p id="cmpnameError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Description</label>
            <textarea class="form-control" rows="3" id="desc" name="desc"></textarea>
            <p id="descError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Url</label>
            <input type="text" class="form-control" name="url" id="url">
            <p id="urlError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Email Id</label>
            <input type="text" class="form-control" name="emailid" id="emailid">
            <p id="emailidError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Address</label>
            <textarea class="form-control" rows="3" id="address" name="address"></textarea>
            <p id="addressError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <h1>All Links <i class="fa fa-link"></i></h1>
        <div class="form-group">
            <label>Facebook Link</label>
            <input type="text" class="form-control" name="fblink" id="fblink">
            <p id="fblinkError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Twitter Link</label>
            <input type="text" class="form-control" name="twlink" id="twlink">
            <p id="twlinkError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
    </div>

<!-- /.col-lg-6 (nested) -->
    <div class="col-lg-6">
        <div class="form-group">
            <label>Linkedin Link</label>
            <input type="text" class="form-control" name="linkinlink" id="linkinlink">
            <p id="linkinlinkError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Download Link</label>
            <input type="text" class="form-control" name="downlink" id="downlink">
            <p id="downlinkError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Live Help Link</label>
            <input type="text" class="form-control" name="livehelplink" id="livehelplink">
            <p id="livehelpError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Terms & Condition Link</label>
            <input type="text" class="form-control" name="tclink" id="tclink">
            <p id="tclinkError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Promotion Link</label>
            <input type="text" class="form-control" name="prolink" id="prolink">
            <p id="prolinkError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Sign Up Link</label>
            <input type="text" class="form-control" name="signuplink" id="signuplink">
            <p id="signuplinkError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Affiliate Link</label>
            <input type="text" class="form-control" name="affiliatelink" id="affiliatelink">
            <p id="affiliatelinkError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <div class="form-group">
            <label>Game Link</label>
            <input type="text" class="form-control" name="gamelink" id="gamelink">
            <p id="gamelinkError" style="display: none; color: red; font-weight: bold;"></p>
        </div>
        <button type="submit" class="btn btn-default" onclick="submitData()"><i class="fa fa-check"></i>Submit</button>
        <button type="reset" class="btn btn-default"><i class="fa fa-refresh"></i>Reset</button>
    </div>  
</form>

Notice that I have a <p> tag just below every input field, whose ID value is constructed from the ID of its corresponding input field. For eg. the <p> tag below text field cmpname is given ID cmpnameError

Now the JavaScript code for displaying the error message below the text field is given below

function submitData()   {
var elements = document.querySelectorAll('#companyDetails input, #companyDetails textarea');

for (var i = 0; i < elements.length; i++)   {
    if (elements[i].value == "")    {
        var errorElementId = "#" + elements[i].id + "Error";
        // alert("Generated ID is " + errorElementId);
        document.getElementById(errorElementId).style.display = '';
        document.getElementById(errorElementId).innerHTML = "Please enter a value for this field";
        return false;
    }
}
document.forms['companyDetails'].submit();
return true;
}

My problem is that the proper error message is not getting displayed on the form when I click submit.

Can anybody please help me regarding this? Thank you very much in advance.

Here is the JSFiddle link- https://jsfiddle.net/v8ooy2e1/

Upvotes: 1

Views: 129

Answers (4)

Frnnd Sgz
Frnnd Sgz

Reputation: 328

If you can use jQuery

  var isValid = true;
$("#companyDetails").submit(function(event) {
    $("input").each(function() {
        var element = $(this);
        if (element.val() == "") {
            isValid = false;

        }
    })
    if (!isValid) {
        alert("nopes");

    }
});

FIDDLE

Upvotes: 0

David P
David P

Reputation: 2083

Why do you have:

var errorElementId = "#" + elements[i].id + "Error";

Leave out the "#" sign.

Upvotes: 0

Rick Hitchcock
Rick Hitchcock

Reputation: 35670

The pound sign is used to select elements with ids using querySelectorAll, but it shouldn't be used with getElementById.

Remove the pound sign here:

var errorElementId = "#" + elements[i].id + "Error";

Should be:

var errorElementId = elements[i].id + "Error";

Working Fiddle

Upvotes: 1

Mike Robinson
Mike Robinson

Reputation: 25159

You know, assuming IE 10+, you could skip all this javascript fanciness and just use the "required" attribute:

<input type="text" class="form-control" id="cmpname" name="cmpname" required>

That'll invoke the browser supported form validation.

Upvotes: 0

Related Questions