On Fri, 29 Oct 2004 13:58:49 +0200, Terje Sæternes <te***@cyberfactory.no>
wrote:
[snip]
<script language="javascript" type="text/javascript">
The language attribute is deprecated, and the (required) type attribute
makes it redundant. Remove it.
<!--
Hiding SCRIPT element content is an obsolete practice. All user agents now
in use understand what a SCRIPT element is, even if they cannot execute
it. Scripts should usually be placed in external files anyway, hiding the
script automatically.
function isblank(s) {
for (var i = 0; i < s.length; i++) {
var c = s.charAt(i);
if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
}
return true;
}
This is a really inefficient way of performing this sort of test. Use a
regular expression:
function isBlank(v) {
return /^\s*$/.test(v);
}
The regular expression will match any string that is either empty, or
solely composed of whitespace characters[1].
function SetRegAction() {
Submission handlers should generally be written to take one argument: a
reference to the form. This means you don't have to hard-code the form
name.
function SetRegAction(form) {
// ...
You'd call this from the FORM with:
<form ... onsubmit="SetRegAction(this);">
Note that you don't need "javascript:". It only makes a difference in one
browser (IE), and even then, its only necessary if you've been using
scripting languages other than Javascript. Most browsers will see that
string a label, and ignore it.
Based on this change, you could modify the following to:
// Save a reference to the 'test' control
var test = form.elements['test'];
if (isblank(document.form1.test.value)) {
if(isBlank(test.value)) {
alert ("Fornavn må fylles ut.");
document.form1.test.focus();
test.focus();
return (false);
return false;
The parentheses aren't necessary.
}
[snip]
<form name="form1" method="post" action=""
Now you don't need to name the form (you should use better names, anyway).
onsubmit="javascript:return SetRegAction()">
<input name="test" type="text" id="test">
You do realise that this control wouldn't be submitted, don't you?
<input type="submit" name="Submit" value="Submit">
Unless you have multiple submit buttons which represent different actions,
you generally don't need to name them. In such situations, something like:
<input type="submit" name="operation" value="Delete">
would be more appropriate. Certainly be careful not to use names that are
properties of the FORM object. Naming a control, action, for example, will
prevent you from accessing the action property on the FORM.
[snip]
Hope that helps,
Mike
[1] A more thorough explanation:
Regular expressions are composed of a series of productions that form a
pattern. Certain characters have special meanings, causing the pattern to
evaluated in a certain way. In the expression I used, there are four
tokens, each of which is special.
The first, ^, means that the pattern that follows must match the beginning
of the input. So, for example, ^some would match any string that began
with "some"; "something", "sometime", "some chicken", etc.
The last, $, means that the pattern that precedes the token must match the
end of the input.
The escape sequence, \s, matches whitespace characters.
The token, *, is a quantity specifier that means the preceding token can
exist zero or more times.
All together, you get a pattern that will match an empty string, or one
which contains only whitespace.
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.