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

Validate forms when users enter spaces 1

Status
Not open for further replies.

benniesanders

Programmer
Jan 20, 2002
199
US
Greetings, I have a lengthy user form and we've noticed some of the site visitors are hitting the spacebar for one of the fields. I have a form validator in place, but if they hit the space bar, it moves on to the next field.

Here's a snippet of my validation code for the rogue field:
Code:
if (myForm.hschool.value == ""){
 alert("High school name is required");
 myForm.hschool.focus();
 return false;}

Is there a way I can combat this?

many thanks. Bennie
 
I have a question. Could any high school in the world be a possible entry into your field, or is it just a few select high schools that would be entered into that field?

(meaning what is the scope of your site?)

If say your site is for London high school graduates, then a <select> box would be a good idea, not only would you force content from that field, it would also be valid content no matter what.

[monkey][snake] <.
 
Hi Monksnake, I wish I could use a dropdown, but no good. It's a nationwide site. It's actually an out of town measurements form for a tuxedo provider :)

Diancecht, I'll read the link the you sent. Thanks very much.
 
CORRECTION. It's a Prom Agent form and covers all high schools, but still it's nationwide for a Tuxedo provider.
 
Dian, I'm SO not a javascript guru. I wouldn't even know where to start with the link you sent; how to modify my code with that trim function. Thank you very much anyway.
 
You stick the prototype declaration somewhere between the <script> tags and then make the following change:
Code:
if (myForm.hschool.value.trim() == ""){

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
uncle_rico_thumb.jpg
 
benniesanders, I'll try to help.

When you ask in your initial post:

>>Is there a way I can combat this?

Do you mean just for not allowing a space to be entered? If so, just change this:

if (myForm.hschool.value == ""){

to this:

Code:
if (String(myForm.hschool.value).replace(/ /g, "") == "")  {

That will not allow any combination of JUST spaces to be entered as a valid School.

[monkey][snake] <.
 
Just make sure that you don't actually replace the value when doing that, otherwise Jersey Community High School becomes JerseyCommunityHighSchool.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
uncle_rico_thumb.jpg
 
Thank you everybody for this help. Monksnake's answer fixed the spaces problem. I don't know why these kids enter info in all of the other fields and not this one and the highschool field is very important. Go figure! Many thanks again to everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top