Unable to load javascrip

T

Terje Sæternes

It seems that this script wont run, any idea what I have don wrong+

Code-----
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
<!--
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;
}



function SetRegAction() {

// WebBrukerID
if (isblank(document.form1.test.value)) {
alert ("Fornavn må fylles ut.");
document.form1.test.focus();
return (false);
}
//-->
</script>
</head>

<body>
<form name="form1" method="post" action="" onsubmit="javascript:return
SetRegAction()">
<input name="test" type="text" id="test">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
 
M

Michael Winter

[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.
 
G

Grant Wagner

Terje Sæternes said:
It seems that this script wont run, any idea what I have don wrong+

Code-----
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">

<script type="text/javascript">

The -language- attribute is deprecated and not required.

Not needed. Remove.
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;
}

function SetRegAction() {

// WebBrukerID
if (isblank(document.form1.test.value)) {
alert ("Fornavn må fylles ut.");
document.form1.test.focus();
return (false);
}

You're missing a closing -}- here.

Not needed. Remove.
</script>
</head>

<body>
<form name="form1" method="post" action="" onsubmit="javascript:return
SetRegAction()">

onsubmit="return SetRegAction();"
<input name="test" type="text" id="test">
<input type="submit" name="Submit" value="Submit">

This is fine, you've named your submit button "Submit". Be careful though,
if you named your submit button "submit" (note the lower case 's') you could
run into problems later if you attempt to submit the form programmatically
(ie - document.forms['theForm'].submit()).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top