Hi all
I have a textarea I would like to validate the same for empty spaces and on no input. Please find my jsfiddle here. How do I validate for empty spaces in the text area? what am I missing in my fiddle?

Thanks
Varun Krishna. P

Recommended Answers

All 6 Replies

Try this:

function validateEmptytextBox() {
    var inputField = document.getElementById("textArea1").value;
    inputField = inputField.replace(/^\s+/, '').replace(/\s+$/, '');
    if(inputField == ""){
        alert("please enter a value, textarea cannot be empty");
    }else{ 
        alert("Success");
    }

}

GliderPilot's answer will work quite well.

If you want to get really fancy, however, you could use a feature of HTML5 called Constraint Validation.

@GliderPilot
Thanks for your reply. Please have a look at my jsfiddle here. No luck it is still not working. What am I missing?

Thanks,
Varun Krishna. P

@Purkinje
Thanks for your reply.

Thanks,
Varun Krishna. P

Line 2 of your JS, your missing the t in Element for your getElementByID

@GliderPilot,
Thanks for pointing out the mistake. I have a small doubt. what are those character(s) doing in the replace() ? Could you please point me somewhere, where I could get some information on those characters(s)/replace function. And why there are two replace () methods?

Thanks,
Varun Krishna. P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.