Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Form 3.4 | Q373 [ previous next ]

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

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>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.