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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Lee SavidgeConnect With a Mentor Commented:
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
 
Lee SavidgeCommented:
Hi,

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

Regards,

Lee
0
 
contactkarthiCommented:
have a look at this

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

for trim rtrim ltrim
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.