Can anybody explain this code pls?

R

roohbir

I was going through this code from Negrino's JavaScript for the WWW
book. I have 2 questions:
1. Why has the author used validForm(passForm)? I mean what is
'passForm' in the function?
2. And its relation to this line - <form onsubmit="return
validForm(this)" action="">; What is 'this' here?

Thanks in advance.
Roohbir



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Password Check</title>
<script language="Javascript" type="text/javascript">
<!-- Hide script from older browsers

function validForm(passForm) {
if (passForm.passwd1.value == "") {
alert("You must enter a password")
passForm.passwd1.focus()
return false
}
if (passForm.passwd1.value != passForm.passwd2.value) {
alert("Entered passwords did not match")
passForm.passwd1.focus()
passForm.passwd1.select()
return false
}
return true
}

// End hiding script -->
</script>
</head>
<body bgcolor="#FFFFFF">
<form onsubmit="return validForm(this)" action="">
Your name: <input type="text" size="30" />
<p>Choose a password: <input type="password" name="passwd1" /></p>
<p>Verify password: <input type="password" name="passwd2" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" /></p>
</form>
</body>
</html>
 
D

David Dorward

roohbir said:
I was going through this code from Negrino's JavaScript for the WWW
book. I have 2 questions:
1. Why has the author used validForm(passForm)? I mean what is
'passForm' in the function?

Just a variable name.
2. And its relation to this line - <form onsubmit="return
validForm(this)" action="">; What is 'this' here?

In that context it is the form, and it gets passed to the specified variable
name in the function.
 
E

Evertjan.

roohbir wrote on 23 okt 2006 in comp.lang.javascript:
I was going through this code from Negrino's JavaScript for the WWW
book. I have 2 questions:
1. Why has the author

You would have to ask him, we can only surmize.
... used validForm(passForm)? I mean what is
'passForm' in the function?

"this" of "validForm(this)"
2. And its relation to this line - <form onsubmit="return
validForm(this)" action="">; What is 'this' here?

"this" represents the object where it called,
in this case the form object.

so:

passForm.passwd1.value

which idealy should have been:

passForm.elements['passwd1'].value

gives the value that could also be described as:

document.forms['thisFormName'].elements['passwd1'].value

or perhaps:

document.getElementById('thisFormId').elements['passwd1'].value

=======
return false

prohibits the form's submission.

=======
<script language="Javascript" type="text/javascript">
<!-- Hide script from older browsers

both lines show that either the book is outdated
or the skils of the writer,
as just this would be optimal in practice and encouraged:

<script type='text/javascript'>
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top