Thursday, December 02, 2004

Maximum characters in a textarea

This is one close to one I developed myself but I can't find it anymore.

<!-- TWO STEPS TO INSTALL LIMIT TEXTAREA:
1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->
<!-- Dynamic 'fix' by: Nannette Thacker -->
<!-- Web Site: http://www.shiningstar.net -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
// End -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<!-- textCounter() parameters are: text field, the count field, max length -->
<center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up to 125 characters. )<br>
<textarea name=message wrap=physical cols=28 rows=4 onKeyDown="textCounter(this.form.message,this.form.remLen,125);" onKeyUp="textCounter(this.form.message,this.form.remLen,125);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125"> characters left</font>
</form>
</center>
<p><center>
<font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 1.37 KB -->

Javascript - set focus on an element but after three letters

<SCRIPT LANGUAGE=JavaScript>
function focusOnField(sField)
{

// Returns the element whose ID is specified.
var tr=document.getElementById(sField).createTextRange();
tr.moveStart('character',3);
tr.select();
// document.getElementById("elementName").focus();
}
// Ende -->
</script>
<input name="elementNamelogin" value="ABC" size="8" maxlength="8" onkeyup="focusOnField(this.form,this);">

Calculate the number of characters in a textarea

Calculate the number of characters in a textarea

<script LANGUAGE="JavaScript">
//onClick event - place in the submit button tag - onclick="return textCounter('element', 'Description', 2000);"

function textCounter(field, fieldname, maxlimit){
var numCharacters = eval('document.frm_daten.'+field+'.value.length');
if (numCharacters > maxlimit) { // if too long
var extraCharacters = numCharacters - maxlimit;
var msg = 'You have entered ' + extraCharacters + ' extra characters in the ' +
fieldname + '. The maximum is ' + maxlimit;
alert(msg);
return false;
}
}
//onChange event place in the textarea tags - onchange="return maxLength('htmlPageTopContainer_frm_daten_inTxtAr_description', 'Description', 2000);"

function maxLength(field, fieldlabel, maxlimit){
var numCharacters = eval('document.frm_daten.'+field+'.value.length');
if (numCharacters > maxlimit) {

var extraCharacters = numCharacters - maxlimit;

var msg = 'You have entered ' + extraCharacters + ' extra characters in the ' +
fieldname + '. The maximum is ' + maxlimit;

alert(msg);
}
}
</script>

Friday, November 12, 2004

Javascript - confirm code - e.g. Are you really sure you want to cancel your changes?

The good old javascript confirm e.g. Are you really sure you want to cancel your changes?

Place this in the header part of the html:

<html>
<head>
<script LANGUAGE="JavaScript">
<!--
// Confirm user wants to cancel the changes
function confirmCancel()
{
var cancelChanges=confirm("Are you sure you wish to cancel your changes?");
if (cancelChanges)
return true ;
else
return false ;
}
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="">
<input TYPE="SUBMIT" name="sub_cancel" value="Cancel" onClick="return confirmCancel();">
</form>
</body>
</html>



Add the text in bold to your submit button.

Thursday, October 21, 2004

Looping through ASP Server Variables

Place this code in the body of your asp file and run it. It will then display all the server variables.

<%

dim obj

for each obj in Request.ServerVariables

response.write "Object Name: " & obj & "<BR>"
& "Object Value: " & Request.ServerVariables(obj) &
"<BR><BR>"

next


%>


Tuesday, October 19, 2004

Configuring generated code and comments - class version in Eclipse

I looked high and low for this and then contacted a guru friend who sent me the code for the version control which is then added to the top of each new class by default.

Add in Window/Preferences/Java/Code Style/Code Templates/Code/New Java files

/*
* ${file_name}
* Created on ${date}
*
* Copyright McTavish Solutions
* Project: Project Name
*/
${package_declaration}

${typecomment}
${type_declaration}

Add in Window/Preferences/Java/Code Style/Code Templates/Comments/Types

/**
* @author ${user}
* @version $$Revision: 1.0 $$
*
*/

Add this in and then CVS will change it with each update made

Wednesday, October 13, 2004

Javascript - Reading variables in another js file

I was asked about this so here is the answwer. You have to include this in your original js

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"
SRC="yourOtherFileName.js"></SCRIPT>

All the variables defined in the other file will now be available to the script. All files and partial scripts are loaded in the page, so that it becomes like an include file. It can however cause problems, such as when 2 global variables have the same name.

Monday, September 27, 2004

Restaurant Puzzler answer...

The answer is that the figures will not add to £30 because they are not from the same equation. Equation 1: What's been paid is £25 for the meal - which is in the till, and £2 for the tip - in the waiter's pocket, leaving the men with £1 each, ie £3, which all adds up to £30. Equation 2: The men have each paid £9 for the meal and the tip together, ie £27, and they each have a £1 in their pocket, ie £3, which all adds up to £30.

Friday, September 24, 2004

Restaurant puzzler...

Three men eat at a restaurant. The bill comes to £25. They each pay £10. When the waiter brings the £5 change they take back £1 each and leave a £2 tip. So each man has paid £9, which totals £27. The waiter has the £2 tip, which makes £29, so where's the other £1 gone?

Answer to follow later

One selected option selects the option in another select box

I was asked by a colleague if this was possible - so I quickly wrote a javascript function. If you want to go by the value of anoption you will have to amend the code slightly.

<html>
<title>One selected option selects the option in another select box</title>
<head>
<script language="JavaScript">
function setSelectedOption() {
//get the number of the selected option - first option is 0
var selectedOption = document.formName.selectName1.selectedIndex;

//Check that one has been selected
if (selectedOption > -1) {
//set the same selected option for the second select box
document.formName.selectName2.options[selectedOption].selected = true;
}

}
</script>
</head>
<body>
<form name="formName"><div align="center">
<p>
<select name="selectName1" size="1"onchange="setSelectedOption();">
<option value="" selected="selected">-----------</option>
<option value="1">First Choice</option>
<option value="2">Second Choice</option>
<option value="3">Third Choice</option>
</select>
</p>
<p>
<select name="selectName2" size="1">
<option value="" selected="selected">---------</option>
<option value="1">First Choice</option>
<option value="2">Second Choice</option>
<option value="3">Third Choice</option>
</select>
</p>
</div></form>
</body>
</html>

Thursday, September 23, 2004

Toggling check box selection

I had to write this code to ensure that if a certain checkbox was selected it deselected another one and vic-versa. Why not use a radio button group I hear you ask, but if more than one select box can be selected from a group of 5 then this is not possible with radio buttons.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Checkbox checking...</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">

function toggleA() {
var materialA = document.getElementsByName("cBox_A")[0];
var materialB = document.getElementsByName("cBox_B")[0];
materialA.checked = true;
materialB.checked = false;

}

function toggleB() {
var materialA = document.getElementsByName("cBox_A")[0];
var materialB = document.getElementsByName("cBox_B")[0];
materialA.checked = false;
materialB.checked = true;

}

</script>
</head>
<body>
<form action="next_file.asp" method="post" enctype="multipart/form-data" name="form_name" id="form_name">
<input name="cBox_A" type="checkbox" value="Value A" onClick="toggleA()">Checkbox A<br>
<input name="cBox_B" type="checkbox" value="Value B" onClick="toggleB()">Checkbox B</form>
</body>
</html>

Tuesday, September 21, 2004

Simple javascript alert

I was just asked by a colleague about a very simple javascript problem - he wanted a confirmation pop up but his code wasn't working properly. So here is the code

script
//confirmation - onclick event
function checkDelete(){
if(confirm("Are you really sure you want to format your hard drive?"))
{
return true;
}
else
{
return false;
}
}

/script

the button code
input name="btn_delete" type="button" id="btn_delete" value="Delete" onClick="return checkDelete();"


He had missed out the return in the onClick event.

Hello World

I thought I'd just give this blogging a go. I read quite a few but have thought it was too much work to maintain it. We will see.