468,316 Members | 2,051 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,316 developers. It's quick & easy.

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

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
5 3851
Dormilich
8,651 Expert Mod 8TB
@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
5,390 Expert Mod 4TB
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
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
8,651 Expert Mod 8TB
@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
5,390 Expert Mod 4TB
@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.

Similar topics

6 posts views Thread by rpgfreak | last post: by
3 posts views Thread by ShaeMills via AccessMonster.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.