Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q373 How can I check that a text box entry is empty or has just spaces?

You are here: irt.org | FAQ | JavaScript | Form | 3.4 | Q373 [ previous next ]

Try:

<SCRIPT LANGUAGE="JavaScript"><!--
function stripSpaces(x) {
    while (x.substring(0,1) == ' ') x = x.substring(1);
    return x;
}

function empty(x) { if (x.length > 0) return false; else return true; }
//--></SCRIPT>

<SCRIPT LANGUAGE="JavaScript1.2"><!--
function stripSpaces(x) { return x.replace(/^\W+/,''); }
//--></SCRIPT>

<FORM NAME="myForm">
<INPUT TYPE="TEXT" NAME="myText">
<INPUT TYPE="BUTTON" VALUE="Test If Empty" onClick="if (empty(stripSpaces(this.form.myText.value))) alert('empty')">
</FORM>

©2018 Martin Webb