By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 1,922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

To check whether user has entered has enter only blank spaces or some text.

P: 2
Hi..
I need to find out whether user has entered only black spaces or some text which might have some spaces..
But i more interested to check for blanck spaces (by spacebar key).
how i will find out this. I used trim function, but it trims text also..i m using my own framework which i built for application.
Plz suggest me somthing..
following functions are not working.
element.length==0 and
element==null
Aug 24 '09 #1
Share this Question
Share on Google+
5 Replies


Dormilich
Expert Mod 5K+
P: 8,639
@Sudhir123
then there’s something wrong with your trim function. you may work around it with String.replace().

note: Firefox 3.5 brings its own trim method.
Aug 24 '09 #2

gits
Expert Mod 5K+
P: 5,370
show your trim function ... normally you trim the value and then check its length ... that is reliable ... but depends on a correct trim function. to check a value that is not empty but just contains whitespaces you could even use a regExp like this:

Expand|Select|Wrap|Line Numbers
  1. var s = '        ';
  2.  
  3. alert(/^[ ]+$/.test(s));
kind regards

PS: :) ahhh ... one minute late ... Dormilich's replace()-hint would work too when you want to work around your trim-function ...
Aug 24 '09 #3

P: 2
Hi guys..
Thanks for ur replies...!! These r really worthful...!!
i found another solution...
I counted blank spaces and put validation for it.!!
Expand|Select|Wrap|Line Numbers
  1.                             var c=0;
  2.                 if (element) {
  3.                       for (var i=0; i<element.length; i++) {
  4.                             if (element.charAt(i) == " ") {
  5.                               c++
  6.                              }
  7.                              }
  8.                              } 
  9.     if(element.length==c)
  10.       return xyz. // according to ur need..!!
Aug 24 '09 #4

Dormilich
Expert Mod 5K+
P: 8,639
@Sudhir123
IMHO a solid trim function is a better choice.

I thought of something like
Expand|Select|Wrap|Line Numbers
  1. if ("function" != typeof String.trim)
  2. {
  3.     String.prototype.trim = function()
  4.     {
  5.         return this.replace(/^\s*/, "").replace(/\s*$/, "");
  6.     }
  7. }
Expand|Select|Wrap|Line Numbers
  1. // use like
  2. var txt = input.value.trim();
Aug 24 '09 #5

gits
Expert Mod 5K+
P: 5,370
@Dormilich
much better ... no question :) ... as a rule of thumb is that loops are to avoid wherever it is possible ...

kind regards
Aug 24 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.