Problem with form field validating

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

    #1

    Problem with form field validating

    on my php page i have a submit button and when I submit i want to
    validate that dome fields have data in.

    i have this as my submit button

    <input name="Enter" type="button" onClick="frmVal idate()" value="Submit">

    and this is my javascript function

    <script language='JavaS cript' type='text/javascript'>
    function frmValidate()
    {
    if (frm.lName.valu e == '')
    {
    alert('Some required fields are empty!');
    } else {
    frm.submit
    }
    }
    </script>

    but it don't work can anyone see where i have gone wrong

    is it because the page is .php instead of .html / .htm

    thanks

    Shane Jones.......... .......
  • Martin Honnen

    #2
    Re: Problem with form field validating



    Shane Jones wrote:
    [color=blue]
    > on my php page i have a submit button and when I submit i want to
    > validate that dome fields have data in.
    >
    > i have this as my submit button
    >
    > <input name="Enter" type="button" onClick="frmVal idate()" value="Submit">[/color]

    How about
    <input type="submit"
    onclick="return frmValidate(thi s.form);"
    name="Enter"
    value="Submit">
    and
    function frmValidate (form) {
    if (form.lName.val ue == '') {
    alert(...);
    return false;
    }
    else {
    return true;
    }
    }
    That way you have a submit button that works even if script is not
    supported or is disabled.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Shane Jones

      #3
      Re: Problem with form field validating

      Martin, thanks for the quick reply

      bit it is still not working

      does it matter that i am using firefox

      Comment

      • Martin Honnen

        #4
        Re: Problem with form field validating



        Shane Jones wrote:
        [color=blue]
        > bit it is still not working
        >
        > does it matter that i am using firefox[/color]

        I posted code that hopefully works with Firefox, IE and other browsers.

        What exactly goes wrong, what does the Firefox JavaScript console say?

        --

        Martin Honnen
        http://JavaScript.FAQTs.com/

        Comment

        • Shane Jones

          #5
          Re: Problem with form field validating

          nothing at all appears in the console

          Comment

          • Lee

            #6
            Re: Problem with form field validating

            Shane Jones said:[color=blue]
            >
            >Martin, thanks for the quick reply
            >
            >bit it is still not working
            >
            >does it matter that i am using firefox[/color]

            No. Post what you're trying now.
            What does "still not working" mean?

            Comment

            • Shane Jones

              #7
              Re: Problem with form field validating

              Hi Lee here is my page code now

              This is for an editing page for database information, it just needs to
              verify that the lName, lAddress1, lTown and lPhoneNo are not empty

              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
              <html>
              <head>
              <title>Insertin g New Location</title>
              <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
              <script language='JavaS cript' type='text/javascript'>
              function frmValidate (form) {
              if (form.lName.val ue == '' || form.lAddress1. value == '' ||
              form.lTown.valu e == '' || form.lPhone.val ue == '') {
              alert('One or more of the required fields are empty.');
              return false;
              }
              else {
              return true;
              }
              }
              </script>

              </head>
              <link href="redifined Tags.css" rel="stylesheet " type="text/css">
              <body background="ima ges/background.jpg" >
              <?
              $No = $_POST['lNo'];
              mysql_connect(' localhost','',' ');
              mysql_select_db ('expense') or die('Error selecting database.');
              $locQuery = "SELECT * FROM locations WHERE lNo = $No";
              ?>
              <?
              $locResult = mysql_query($lo cQuery);
              while ($lRow = mysql_fetch_ass oc($locResult))
              {
              extract($lRow);
              ?>

              <h4>Editing <? echo $lName ?></h4>
              <p>Use this below form to edit details about this item.</p>
              <hr>
              <form action="php_scr ipts/replace_locatio n_script.php" method="post">
              <table width="70%" border="0" cellspacing="0" cellpadding="0" >
              <tr>
              <td width="29%" height="30"><p> Name</p></td>
              <td width="70%">
              <input name="rNo" type="hidden" size="20" maxlength="30"
              value="<? echo $lNo ?>">
              <input name="rName" type="text" size="45" maxlength="30"
              value="<? echo $lName ?>"><font size="-3" face="Verdana"
              color="#000099" >req.</font>
              </td>
              </tr>
              <tr>
              <td height="30"><p> Address</p></td>
              <td>
              <input name="rAddress1 " type="text" size="45" maxlength="30"
              value="<? echo $lAddress1 ?>"><font size="-3" face="Verdana"
              color="#000099" >req.</font>
              </td>
              </tr>
              <tr>
              <td height="30"></td>
              <td>
              <input name="rAddress2 " type="text" size="45" maxlength="30"
              value="<? echo $lAddress2 ?>">
              </td>
              </tr>
              <tr>
              <td height="30"><p> Town</p></td>
              <td>
              <input name="rTown" type="text" size="20" maxlength="15"
              value="<? echo $lTown ?>"><font size="-3" face="Verdana"
              color="#000099" >req.</font>
              </td><td width="1%">
              </tr>
              <tr>
              <td></td>
              <td></td>
              </tr>
              <tr>
              <td height="30"><p> Phone</p></td>
              <td>
              <input name="rPhoneNo" type="text" size="20" maxlength="15"
              value="<? echo $lPhoneNo ?>"><font size="-3" face="Verdana"
              color="#000099" >req.</font>
              </td>
              </tr>
              </table>
              <?
              }
              mysql_free_resu lt($locResult);
              ?>
              <br>
              <input name="Enter" onClick="return frmValidate(thi s.form);"
              type="submit" value="Update">
              <input name="Reset" type="reset" value="Reset">
              </form>
              </body>
              </html>
              --
              Shane Jones.......... .......
              shanemjo82@yaho o.co.uk <mailto:shanemj o82@yahoo.co.uk >

              Comment

              • Martin Honnen

                #8
                Re: Problem with form field validating



                Shane Jones wrote:

                [color=blue]
                > function frmValidate (form) {
                > if (form.lName.val ue == '' || form.lAddress1. value == '' ||[/color]
                ^^^^^^
                Here you are looking for controls with the name lName, lAddress and so
                forth but below in the form you use the names

                [color=blue]
                > <input name="rName" type="text" size="45" maxlength="30"[/color]
                ^^^^^
                rName and rAddress and so forth. So perhaps it is only a naming issue.


                --

                Martin Honnen
                http://JavaScript.FAQTs.com/

                Comment

                • Lee

                  #9
                  Re: Problem with form field validating

                  Shane Jones said:[color=blue]
                  >
                  >Hi Lee here is my page code now
                  >
                  >This is for an editing page for database information, it just needs to
                  >verify that the lName, lAddress1, lTown and lPhoneNo are not empty
                  >
                  ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                  ><html>
                  ><head>
                  ><title>Inserti ng New Location</title>
                  ><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                  ><script language='JavaS cript' type='text/javascript'>
                  >function frmValidate (form) {
                  > if (form.lName.val ue == '' || form.lAddress1. value == '' ||
                  >form.lTown.val ue == '' || form.lPhone.val ue == '') {
                  > alert('One or more of the required fields are empty.');
                  > return false;
                  > }
                  > else {
                  > return true;
                  > }
                  > }
                  ></script>[/color]


                  [color=blue]
                  ><form action="php_scr ipts/replace_locatio n_script.php" method="post">[/color]
                  [color=blue]
                  > <input name="Enter" onClick="return frmValidate(thi s.form);"
                  >type="submit " value="Update">[/color]

                  1. You don't have form elements named lName, lAddress1, etc.
                  2. This sort of form validation function should be invoked from
                  the onSubmit handler of the form element, never from the onClick
                  handler of the submit button:

                  <form action="php_scr ipts/replace_locatio n_script.php"
                  method="post"
                  onsubmit="retur n frmValidate(thi s)">

                  Comment

                  Working...