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

Help validating a form

Status
Not open for further replies.

DataSpy

Technical User
Joined
Nov 15, 2002
Messages
39
Location
US
Hello, I'm having troulbe validating a form. I found this script on a website, but it doesn't seem to be working.

<!-- start html & script -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript">
function validate() {
1 = document.form.1.value;
if ((1 == "")) {
alert("Please enter your name or your email address.");
return false;
}
else return true;
}
</script>
</head>

<body>
<form action="index.html" method="post" name="form" onSubmit="return validate()">
<input type='text' name='1' value="" onFocus="document.form.1.value='';"><br>
<input type='submit' value='tst'>
</form>
</body>

</html>
<!-- end html & script -->
Any help would be greatly appretiated & thanx in advance!
 
Don't start either variable names or element names/IDs with numbers. Some browsers will deal with element names/IDs that begin with a number but some won't.

1 = document.form.1.value; will be treated as an invalid assignment and you can't call your input field '1' either.

 
Also, for maximum compatibility, use document.forms['form'].elements['1'].value as opposed to document.form.1.value.

There may also be a problem with a form named "form".


--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
I've never noticed a problem with a form called 'form'.

Nor have I noticed a problem with dot-string syntax anywhere. document.form should work fine afaik.
 
Dot-string syntax works in MSIE, but causes errors in Firefox and more.

There will definitely be a problem with forms called "forms".

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
chessbot -

unfortunately i just lost this same argument (i was on your side) on another website. it was literally 1 hour ago.

however, to be most readable, i will always vow to use the form:

Code:
document.forms['form_name'].elements['elem_name'];



to test, i created a form like this:

Code:
<form name="form">
  <input type="text" name="value">
  <input type="button" onclick="alert(document.form.value.value);">
</form>

and grossly, it seemed to work fine, even in NS 4.

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
cLFlaVA:
Works in my FireFox browser. Odd, no?

Everyone:
----------------------
Use associative arrays ([tt]document.forms['myform'][/tt]) instead of dot-name syntax ([tt]document.myform[/tt])! If nothing else, it lets you use a variable in the form's name without using [tt]eval()[/tt]. Also, it is more aesthetically pleasing.

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
chessbot - yeah, it worked for me in all browsers.

document.garbage.filth

seems to be valid.

You make a good point, however, by describing the use of variable names with the preferred method.

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Hehe ... It's a plot to make people use them ... that happens to be true ... but it is still a plot ... or better, an incentive ... I need more statements so I can use more ellipses ... ... ... ... ... ... .

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Yay! I'm glad this is settled at last. Does this mean you're gonna stop beating up on dot-string all the time? :D

Chessbot, I didn't say there wouldn't be a problem with a form called "forms"; it was called "form" which works afaik.

 
Maybe we won't beat up on it as much, but we will most likely still suggest using [].[] as the preferred method :)

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Fine with me. It's my preferred method, too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top