Check form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wj

    Check form

    Hi all,

    Im trying to, deal with a problem that drives me crazy!

    Im useing the following code to chech the fields of a form before
    really submitting them:

    -------
    <script language="JAVAS CRIPT">

    function CheckForm()
    {
    var f = document.regist er;
    var error_mes = '';
    var submitOK="True" ;
    if (f.place.value. length < 3)
    {
    error_mes = error_mes + "\n - No place.";
    SubmitOK = "False";
    }
    if (f.provi.option s[f.provi.value].value == 0)
    {
    error_mes = error_mes + "\n - No province.";
    SubmitOK = "False";
    }
    if (f.educa.option s[f.educa.value].value == 0)
    {
    error_mes = error_mes + "\n - No education.";
    SubmitOK = "False";
    }
    if (f.prof.value.l ength == ' ')
    {
    error_mes = error_mes + "\n - No profession.";
    SubmitOK = "False";
    }
    if (f.civil.option s[f.civil.value].value == 0)
    {
    error_mes = error_mes + "\n - No civil.";
    SubmitOK = "False";
    }
    if (f.pass.value.l ength == ' ')
    {
    error_mes = error_mes + "\n - No password.";
    SubmitOK = "False";
    }
    if (f.pass.value != f.pass_ch.value )
    {
    error_mes = error_mes + "\n - No password check.";
    SubmitOK = "False";
    }
    if (f.email_1.valu e.length < 4)
    {
    error_mes = error_mes + "\n - No e-mail.";
    SubmitOK = "False";
    }
    if (f.email_1.valu e != f.email_2.value )
    {
    error_mes = error_mes + "\n - No e-mail check.";
    SubmitOK = "False";
    }
    if (SubmitOK=="Fal se")
    {
    alert(error_mes );
    return false
    }
    }
    </script>
    ---------

    When i leave all the field empty and don't use the dropdown-boxes,
    everything works fine; I get an error message and the form is not
    submitted. But when i enter text or use the first three or four field
    and leave the others blank, i don't get an errormessage and the data
    is submitted.
    Can anyone tell me how to deal with it??

    Thanx!!
    WJ
  • Ivo

    #2
    Re: Check form

    "wj" wrote[color=blue]
    > if (f.prof.value.l ength == ' ')
    > if (f.civil.option s[f.civil.value].value == 0)
    > if (f.pass.value.l ength == ' ')[/color]

    The length property always returns a number, sometimes 0, sometimes more. A
    value is always a string, sometimes an empty string (of length 0), sometimes
    a string which represents a number, such as "0" or "123". You probably want
    to change the above lines to

    if (f.prof.value.l ength == 0)
    if (f.civil.option s[f.civil.value].value == '')
    if (f.pass.value.l ength == 0)

    HTH
    Ivo


    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Check form

      wj wrote:[color=blue]
      > <script language="JAVAS CRIPT">[/color]

      <script type="text/javascript">
      [color=blue]
      > function CheckForm()
      > {
      > var f = document.regist er;[/color]

      function CheckForm(f)
      {
      f = f.elements;
      if (f)
      {
      [color=blue]
      > var error_mes = '';
      > var submitOK="True" ;[/color]

      var submitOK = true;
      [color=blue]
      > if (f.place.value. length < 3)[/color]

      if (f['place'].value.length < 3)

      [color=blue]
      > {
      > error_mes = error_mes + "\n - No place.";
      > SubmitOK = "False";[/color]

      error_mes += "\n - No place.";
      SubmitOK = false;
      [color=blue]
      > }
      > if (f.provi.option s[f.provi.value].value == 0)[/color]

      if (f['provi'].options[f['provi'].selectedIndex].value == 0)
      [color=blue]
      > {
      > error_mes = error_mes + "\n - No province.";
      > SubmitOK = "False";[/color]

      See above.
      [color=blue]
      > }
      > if (f.educa.option s[f.educa.value].value == 0)[/color]

      if (f['educa'].options[f['educa'].selectedIndex].value == 0)
      [color=blue]
      > [...] {
      > error_mes = error_mes + "\n - No education.";
      > SubmitOK = "False";[/color]

      See above.
      [color=blue]
      > }
      > if (f.prof.value.l ength == ' ')[/color]

      `length' is a number property and as such never equals ' '.
      You probably meant

      if (f['prof'].value.length == 0)

      where f['prof'].value would be the *empty* string ('').
      [color=blue]
      > {
      > error_mes = error_mes + "\n - No profession.";
      > SubmitOK = "False";[/color]

      See above.
      [color=blue]
      > }
      > if (f.civil.option s[f.civil.value].value == 0)[/color]

      See above.
      [color=blue]
      > {
      > error_mes = error_mes + "\n - No civil.";
      > SubmitOK = "False";[/color]

      See above.
      [color=blue]
      > }
      > if (f.pass.value.l ength == ' ')[/color]

      See above.
      [color=blue]
      > [...]
      > if (SubmitOK=="Fal se")[/color]

      if (! SubmitOK)
      [color=blue]
      > {
      > alert(error_mes );[/color]
      }
      }
      return SubmitOK;
      [color=blue]
      > }
      >
      > </script>
      > ---------[/color]

      <form action="..." name="register" onsubmit="retur n CheckForm(this) ">
      ...
      </form>
      [color=blue]
      > When i leave all the field empty and don't use the dropdown-boxes,
      > everything works fine; I get an error message[/color]

      What error message that was may have been important.
      [color=blue]
      > and the form is not submitted.[/color]

      Of course.
      [color=blue]
      > [...][/color]


      PointedEars

      P.S.
      The word "I" is always capitalized in English.

      P.P.S.
      Your From: address is invalid:
      | 550 5.1.1 <wzn@jongnederl and.nl>... User unknown

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Check form

        Thomas 'Ingrid' Lahn wrote:[color=blue]
        > wj wrote:[color=green]
        >> var error_mes = '';
        >> var submitOK="True" ;[/color][/color]
        ^^^^^^^^[color=blue]
        >
        > var submitOK = true;[/color]
        ^^^^^^^^[color=blue]
        >[color=green]
        >> if (f.place.value. length < 3)[/color]
        >
        >
        > if (f['place'].value.length < 3)
        >
        >
        >[color=green]
        >> {
        >> error_mes = error_mes + "\n - No place.";
        >> SubmitOK = "False";[/color][/color]
        ^^^^^^^^[color=blue]
        >
        > error_mes += "\n - No place.";
        > SubmitOK = false;[/color]
        ^^^^^^^^
        Note that ECMAScript and its implementations are case-sensitive.
        If should be *always* either `SubmitOK' or `submitOK'.


        PointedEars

        Comment

        Working...