Listbox Select All for Multiple/Multiselect

Need a simple way to select all or none of the options?

This code is for ASP.NET!

This function needs to be placed once near the top of the page:

[js]
<script type="text/javascript">
/* jak’s select all code */
function lbselectall(listboxname, all) {
sb = document.getElementById(listboxname);
for (var i = 0; i < sb.options.length; i++) {
sb.options[i].selected = all;
}
}
</script>
[/js]

Then for each ListBox, simply put this snippit of HTML below it and edit ListBox1 to reflect the name of the listbox!

[html]
<a href="javascript:lbselectall(‘<%=ListBox1.ClientID %>’, true)">all</a>
<a href="javascript:lbselectall(‘<%=ListBox1.ClientID %>’, false)">none</a>
[/html]

For a full example, look at this code:

[html]
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem>one</asp:ListItem>
<asp:ListItem>two</asp:ListItem>
<asp:ListItem>three</asp:ListItem>
<asp:ListItem>four</asp:ListItem>
</asp:ListBox><br />
<a href="javascript:lbselectall(‘<%=ListBox1.ClientID %>’, true)">all</a>
<a href="javascript:lbselectall(‘<%=ListBox1.ClientID %>’, false)">none</a>
[/html]

Hope that help

Really basic form validation

As I know this will help someone, here’s a simple way to use form validation.

One important bit: The function to go in the <head>.

[code lang=”js”]

[/code]

And make sure you text boxes have an "ALT" attribute, like this for example:
<input type="text" name="fname" id="fname" alt="First Name" />
Important! Only put an ALT attribute for the required inputs!

Finally, tuck this little bit into your <form> tag you want to use:
<form name="contactform" onsubmit="return(checkForm(this));" method="..  >

Processing Forms with JavaScript

Occasionally you can’t use server-side languages to deal with form input. In my case it’s because my university coursework assignment had to be done in JavaScript, but who knows what your reasons could be.

This snippet of code has a few good points:

  • It’ll take standard forms with no javascript and parse the inputs.
  • You can use the GET method to send the data.
  • It’s simple, short and cross-browser compatible.

And some bad..

  • You can’t handle submitted files (not really tested it though)
  • You can’t do things with POST’ed forms.
  • You really shouldn’t be processing anything important on the client-side, as it’s open to a multitude of attacks.

But anyway, I shall walk you through my thinking…

Continue reading Processing Forms with JavaScript

Search box friendly-ness

A way to save space, but still make your search box clear and usable.

This is a simple addition to have the search box ( or whatever you like really! ) contain the label for the input box. In this instance, you can have the text in the search box disappear when you click it.

In your <head> tag:
[code lang=”js”]

[/code]
And on your text box:

[code lang=”html”]

[/code]

A way to save space, but still make your search box clear and usable.

 

Example: