Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a textbox is empty using javascript

Tags:

javascript

I have a jsp page with some TextBoxes. Now I want to fill them with some information and click the submit button. But I need to check whether this TextBox is empty or not.

How can I do this?

like image 817
user222585 Avatar asked Aug 17 '10 12:08

user222585


1 Answers

Using regexp: \S will match non whitespace character:anything but not a space, tab or new line. If your string has a single character which is not a space, tab or new line, then it's not empty. Therefore you just need to search for one character: \S

JavaScript:

function checkvalue() { 
    var mystring = document.getElementById('myString').value; 
    if(!mystring.match(/\S/)) {
        alert ('Empty value is not allowed');
        return false;
    } else {
        alert("correct input");
        return true;
    }
}

HTML:

<form onsubmit="return checkvalue(this)">
    <input name="myString" type="text" value='' id="myString">
    <input type="submit" value="check value" />
</form>
like image 112
sweets-BlingBling Avatar answered Nov 08 '22 08:11

sweets-BlingBling