Can anybody explain this code pls?

Discussion in 'Javascript' started by roohbir, Oct 23, 2006.

  1. roohbir

    roohbir Guest

    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>
    roohbir, Oct 23, 2006
    #1
    1. Advertising

  2. roohbir wrote:

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

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
    David Dorward, Oct 23, 2006
    #2
    1. Advertising

  3. roohbir

    Evertjan. Guest

    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'>


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Oct 23, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Tmljaw==?=
    Replies:
    2
    Views:
    436
    =?Utf-8?B?Tmljaw==?=
    Jan 11, 2006
  2. Replies:
    2
    Views:
    336
    VisionSet
    May 24, 2006
  3. Replies:
    0
    Views:
    238
  4. Roberto
    Replies:
    0
    Views:
    388
    Roberto
    Nov 6, 2006
  5. Replies:
    1
    Views:
    292
    Victor Bazarov
    Oct 22, 2007
Loading...

Share This Page