Dave's Brain

Browse - Programming Tips - Is there a robust way to check a string for being empty?

Date: 2014nov16
Language: javaScript

Q.  Is there a robust way to check a string for being empty?

A.  These functions handle undefined, null, zero length and ignore leading and trailing space

function isSpace(a) { if (a === undefined || a == null) return true; if (a.length == 0) return true; return a.match(/^\s*$/); } function hasContent(a) { return ! isSpace(a); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.