Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation: blank spaces in a text box

Status
Not open for further replies.

scroce

MIS
Nov 30, 2000
780
US
Two part question:

1. What's the difference between these three ifs: (pseudo code - not to be taken literally)

function Validate(txtBox) {
if txtBox.value==""
if txtBox.value==null
if txtBox.length == 0
return false;
}
}

2. None of these seem to be able to check for a blank space (i.e. if someone just hits the space bar once or several times) How do you validate for someone doing this?

Seek not outside yourself; heaven is within.
 
do it this way
<html>
<head>
<script>
function Val() {
if (form1.txtBox.value==&quot;&quot;) {
alert(&quot;box empty!&quot;);
return false;
} }
</script>
</head>
<body>
<form name=&quot;form1&quot;>
<input type=&quot;text&quot; name=&quot;txtBox&quot; onBlur=&quot;Val()&quot;>
</form>
</body>
</html>

or

<html>
<head>
<script>
function Val(box) {
if (box.value==&quot;&quot;) {
alert(&quot;box empty!&quot;);
return false;
} }
</script>
</head>
<body>
<form name=&quot;form1&quot;>
<input type=&quot;text&quot; onBlur=&quot;Val(this)&quot;>
</form>
</body>
</html> You cannot mandate productivity, you must provide the tools to let people become their best.
-Steve Jobs
admin@onpntwebdesigns.com
 
The difference between what you referenced are just checking for different data types. in javascript

Number 0
String &quot;&quot;
Boolean true, false
Null null
Undefined undefined

seeing as 0 is the same as &quot;&quot; in all essence you will get the alert to come up, BUT if you place 0 in the box then it will also. Bad you use this for validatation purposes for that reason.
null checks for value period. you use this to check for objects like variables etc. example
if (form1.txtBox2 == null) {
alert(&quot;No box to check!&quot;);

there is no Box2 so you get alerted.

hope that helps You cannot mandate productivity, you must provide the tools to let people become their best.
-Steve Jobs
admin@onpntwebdesigns.com
 
but I seem to be observing if you do this:

function Val() {
if (form1.txtBox.value==&quot;&quot;) {
alert(&quot;box empty!&quot;);
return false;
} }

then it still does not capture if the user hits the space bar. How do you get around that? Seek not outside yourself; heaven is within.
 
Maybe this might do the trick for the spacebar:
Code:
function checkforspacebar() {
  if(event.keyCode == 32) {
    alert(&quot;Space bar detected&quot;);
  }
}
And call this function like this:
Code:
<form>
<input type=&quot;text&quot; onkeypress=&quot;return checkforspacebar();&quot;>
</form>
If i'm right, then this will throw an alert when a spacebar is pressed... I have not failed; I merely found 100,000 different ways of not succeding...
 
And if you want to restrict the user from entering the spacebar at all, just change the alert message to return false I have not failed; I merely found 100,000 different ways of not succeding...
 
scroce,

if you want to check to see if the string is nothing but spaces, you can do something like this:
[tt]
function isAllSpaces(sText) {
var flag = true;
for (idx = 0; idx < sText.length; idx++)
if (sText.charAt(idx) != &quot; &quot;)
flag = false;
return flag;
}
[/tt]
or a little more elegant with a regular expression:
[tt]
function isWhiteSpace(sText) {
var pat = /^\s+$/;
return pat.test(sText);
}
[/tt]
=========================================================
if (!succeed) try();
-jeff
 
thanks everyone for you posts - i'll try it Seek not outside yourself; heaven is within.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top