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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

blank space validation 1

Status
Not open for further replies.

scotth4v

Programmer
Apr 17, 2001
103
0
0
US
A long time ago, I saw a post (that I can't find now) about someone wanting to validate against all blank spaces in their fields... they had done it by using a huge, ugly, long if comparison..

if ((myvar == ' ') || (myvar == ' ') || (myvar == ' ')...) ect.

I was really busy at the time, so I didn't write a response, but I found myself needing the same routine this morning and thought I'd share my solution... (I'll make it a faq after it gets the inevitable polish from you guys... :)

Code:
form1 = document.forms[0];
myvar = form1.myfield.value ;

if (myvar.length == 0){
   alert('Please enter a Description');
   form1.myfield.focus();
   return false;
} else if (myvar.length > 0){
   spaces = '';
   for(var i = 0; i < 20; i++){ //20, an arbitrary setting
	spaces = spaces + ' ';
	if (myvar==spaces){			
	    alert('all spaces');
	    form1.myfield.focus();
	    return false;
	}
    }
}

return true;

I tried using a myvar.charCodeAt(i) and looking at each character and setting a flag if all were spaces (32) but for some reason, I couldn't compare against that value... &quot;if (myvar.charCodeAt(i)== 32) &quot; would always show true!? (ie 6)

-Scott
 
that still only validates against a single blank... I had lazy feild-techs entering 2-3 spaces to get around my validation! :)

a helpful reference though on the radio buttons and such!

Thanks!
 
Oups...sorry, didn't see the &quot;ALL&quot; blank spaces...

;-)

Have Fun...

Sharky99 >:):O>
 
hahaha, np, that is a useful site! I can't believe my techs would sit there and hit space repetedly to see how many spaces I was checking for to avoid writing a small description... kinda like the velociraptors in Jurassic Park... just keep hitting that electric fence! hahahah :)
 
Has anybody heard of regular expressions??
Code:
var re=/^\s*$/;
if (re.test(form1.myfield.value))
 alert(&quot;You didn't fill out the form correctly, you idiot&quot;);
bluebrain.gif
blueuniment.gif
 
actually no. hahaha learn something new everyday...
 
Nice Uniment worth a star... ;-)
Have Fun...

Sharky99 >:):O>
 
If U want to trim what user enters in the textbox use this:

form1.myfield.value=(form1.myfield.value).replace(/(^\s*)|(\s*$)/g, &quot;&quot;)

This will trim the blank spaces on either side of the text entered..

Cheers!

srinu
 
Ya, I don't come from a traditional programming background, so I hadn't come across them yet. That's a big help... it's still confusing to read to me, but I printed out the syntax reference I have and I'm sure it'll just take some getting used to.

Thanks a bunch

-Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top