Check empty textbox

Hi,
I want to check whether the password textbox is empty or not? Sometimes user may input only blank spaces by pressing spacebar key. Therefore the code txtPwd.value == '' will not be true. So I guess, I will have to use trim() and then i will have to use the code txtPwd.value == ''  and then it becomes true. But trim() didn't work for me. So can anyone help me out by giving the full code?

Thanks.

Miran
Gemini532Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
Hi,

Javascript does not have a trim function. You need to test for txtPwd.length == 0;

Regards,

Lee
0
Lee SavidgeCommented:
Hi,

If you don't allow just spaces you could try:

txtPwd.replace(/^\s*/, '').replace(/\s*$/, '') == '';

This will remove leading and trailing spaces.

Regards,

Lee
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
contactkarthiCommented:
have a look at this

http://www.somacon.com/p355.php

for trim rtrim ltrim
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Gemini532Author Commented:
Thanks for your reply but Sorry Lee, This did not work. Let me give you the  code snippet

var test = document.getElementById("txtPwd").value.length;
alert (test);

if(document.getElementById("txtPwd").value.length == 0)
{
      alert("Password cannot be blank");
      form1.txtPwd.focus();
}




See  I have typed in the textbox 4 blank spaces by pressing SPACEBAR 4 times and the length is shown as 4, which I dont want. I want that tobe zero since it is blank space only.


0
BadotzCommented:
Try:

if(document.getElementById("txtPwd").value == '')
0
Gemini532Author Commented:
Thanks. Its worked great
0
BadotzCommented:
Here is a little string lib for javascript:
/*
 * ####################################
 * string.js
 * ------------------------------------
 * ltrim
 * rtrim
 * trim
 * chop
 * pad
 * ####################################
 */
 
var X_STRING = function()
{ 
	return {
		'ltrim' : function(val) { return ((typeof val == 'string') ? val.replace(/\s*((\S+\s*)*)/, '$1') : ''); },
		'rtrim' : function(val) { return ((typeof val == 'string') ? val.replace(/((\s*\S+)*)\s*/, '$1') : ''); },
		'trim'  : function(val) { return ((typeof val == 'string') ? X_STRING.ltrim(X_STRING.rtrim(val)) : ''); },
		// ----------------------------
		/* chop str into array  */
		'chop'  : function chop(str, len, glue)
		{
			var out = [];
			if (typeof str == 'string')
			{
				len = ((len === undefined || isNaN(len)) ? str.length : parseInt(len));
				while (str != '')
				{
					if (str.length > len)
					{
						var tmp = String(str).substr(0, len);
						var x = tmp.lastIndexOf(' ');
						if (x > 0)
						{
							out.push(X_STRING.trim(tmp.substr(0, x)));
							str = X_STRING.trim(str.substr(x + 1));
						}
						else
						{
							out.push(tmp.substr(0, len));
							str = str.substr(len + 1);
						}
					}
					else
					{
						out.push(str);
						str = '';
					}
				}
			}
//			if (glue === undefined)
//			{
//				return out;
//			}
//			else
//			{
//				return out.join(glue);
//			}
			return ((glue === undefined) ? out : out.join(glue));
		},
		// ----------------------------
		/* pad str to len w/chr */
		'pad'   : function(val, mod, len, spc)
		{
			if (typeof val == 'string')
			{
				var valu = ((val === undefined) ? '' : val);
				var mode = ((mod === undefined) ? 'R' : mod); // pad right (default)
				var plen = ((len === undefined || isNaN(len)) ? valu.length : parseInt(len));
				var padr = ((spc === undefined) ? ' ' : String(spc).substr(0, 1));
				if (plen < String(valu).length) { return valu.substr(0, plen); } // chop it
				plen -= valu.length;
				switch (mode.toUpperCase())
				{
					case 'C' : 
						var p = z(); 
						plen = parseInt(plen / 2); 
						return p.substr(0, plen) + valu + p.substr(plen);
					case 'L' : return z() + valu;
					default  : return valu + z();
				}
			}
			return this.pad('', 'r', len, spc); // all else fails
			// ----------------------------
			function z() { var x = ''; for (var i = 0; i < plen; i++) { x += padr; }; return x; }
			// ----------------------------
		}
	};
}();

Open in new window

0
contactkarthiCommented:
the follwing code will do the trick for you
<html>
<head> 
<script type="text/javascript" language="JavaScript"> 
function validate_form() {
var test = document.getElementById("txtPwd").value.length;
alert (test);
document.getElementById("txtPwd").value = trim(document.getElementById("txtPwd").value);
test = document.getElementById("txtPwd").value.length;
alert (test);
if(document.getElementById("txtPwd").value.length == 0)
{
      alert("Password cannot be blank");
      form1.txtPwd.focus();
}
} 
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
} 
</script> 

</head>
<body>
<form action="#" method="POST" name="myForm" >
<input type="text" id=txtPwd name="myText1" onBlur="validate_form()"> 
</form>
</body>

Open in new window

0
Lee SavidgeCommented:
Glad to help.

Lee
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.