Finding an empty input on text field


On my ongoing project,I allow user to add text to the application.Once the user presses the “Add Button” on my app,an input text field is added along with user input.
So,I needed to check whether the user input is empty or not.I googled and found some solution.But when I used it ,they weren’t the effective ones.

//the solution were like this
if(inputText.text.length>0)
{
//then perform some action
}
OR
if(inputText.text ="")
{
//alert user that input text is empty
}

These two method works well when user doesn’t input anything and clicks the add button.But if the user do the following
Case I
The user just presses spaces or spacebar and then press add button

Case II
The user writes something and then deletes all of it and presses space

Then both of above logic to find empty input fails.

So I again googled for some String utility classes and finally found one

http://code.ericfeminella.com/classes/as3/StringUtils.as.html

which contained a method called trimWhiteSpace.
This method does is replaces all the whiteSpace or say spaces of the input text and returns the length of the text without white space.
So this method proved to be the best for finding out the empty input on text field.

function trimWhiteSpace(source:String) :uint
{
var pattern:RegExp = /[\s]+/g;
return source.replace(pattern,"").length;
}
if(trimWhiteSpace(inputText.text) ==0)
{
//alert user for empty input
}

Hope this helps for all to find out whether the input text is empty or not

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s