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

comparing multiple string

Status
Not open for further replies.

TurboSRT4

Programmer
Nov 23, 2009
47
US
hi im sorry for seemingly easy questions but im learning javascript and am already good at perl.

example i have a form name registerForm
how can i make sure no fields are blank and alert if one is blank returning its name

alert(value' is blank');

that is not really what im trying to do but the answer to that question would answer my more complicated question for me im just tryin to make it as easy as possible and simple for you guys to help thanks
 
Hi

Ok, you are beginner. But you could search the web or just this forum for some examples.

Anyway, this is how I prefer it :
JavaScript:
[b]var[/b] empty[teal]=[][/teal]

[b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal];[/teal]i[teal]<[/teal]theform[teal].[/teal]elements[teal].[/teal]length[teal];[/teal]i[teal]++)[/teal]
  [b]if[/b] [teal](![/teal]theform[teal].[/teal]elements[teal][[/teal]i[teal]].[/teal]value[teal])[/teal]
    empty[teal].[/teal][COLOR=darkgoldenrod]push[/color][teal]([/teal]theform[teal].[/teal]elements[teal][[/teal]i[teal]].[/teal]name[teal])[/teal]

[b]if[/b] [teal]([/teal]empty[teal].[/teal]length[teal])[/teal]
  [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'The following '[/i][/green][teal]+[/teal]empty[teal].[/teal]length[teal]+[/teal][green][i]' fields are blank :[/i][/green][lime][i]\n[/i][/lime][green][i]- '[/i][/green][teal]+[/teal]empty[teal].[/teal][COLOR=darkgoldenrod]join[/color][teal]([/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]- '[/i][/green][teal]))[/teal]
Note that I only answered your question. In case of [tt]checkbox[/tt] and [tt]radio[/tt] [tt]input[/tt]s is pointless to check if they are blank.

Feherke.
 
wow man that is great and simple! I am good with perl so i can take your little example and stem a lot of knowledge off of it thank you!
 
Hi

Now I see that I forgot to add the comment I intended on theform.

It is just a regular variable and should hold a reference to a [tt]form[/tt] element. You must set it before running that code :
JavaScript:
theform[teal]=[/teal]document[teal].[/teal]your_froms_name

[gray]// or[/gray]

theform[teal]=[/teal]document[teal].[/teal]forms[teal][[/teal]your_forms_order_number[teal]][/teal]

[gray]// or[/gray]

theform[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'your_forms_id'[/i][/green][teal])[/teal]
But the most frequent way is to pass the reference from the [tt]onsubmit[/tt] event :
HTML:
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]""[/i][/green] [maroon]onsubmit[/maroon][teal]=[/teal][green][i]"return checkmyform(this)"[/i][/green][b]>[/b]
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]checkmyform[/color][teal]([/teal]theform[teal])[/teal]
[teal]{[/teal]
  [gray]// the code from my first post comes here[/gray]
  [gray]/*
    note that theform is a parameter in this case,
    so the above mentioned assignments are not necessary in this case
  */[/gray]

  [b]return[/b] empty[teal].[/teal]length[teal]==[/teal][purple]0[/purple]
[teal]}[/teal]
Note that I added one more thing, the [tt]return[/tt] statement. It returns [tt]false[/tt] if at least one blank field was found. If the JavaScript code executed by [tt]onsubmit[/tt] returns [tt]false[/tt], the browser will not submit the [tt]form[/tt].


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top