Jump to content

Recommended Posts

FormCheck not working

 

<head>
<script language="JavaScript">
function checkForm()
{
   if($company) == '')
   {
      alert('Please enter your Company Name);
      return false;
   }
}
</script>
</head>

 

      <form action="post.php" onSubmit="return checkForm(this)" method="POST">
        <table align=center width="575" border=0 cellspacing=10>
          <tr>
            <td align=right><b>Name of Company</b> </td>
            <td><input type="text" name="company" size="50">
            </td>
          </tr>
          <tr>
            <td align=center colspan="2"><input type="submit" name="Submit" value="Submit Form">
            </td>
          </tr>
</table>

 

Any ideas?

 

Regards,

DED

Link to comment
https://forums.phpfreaks.com/topic/140676-form-validation/
Share on other sites

FormCheck not working

 

<head>
<script language="JavaScript">
function checkForm()
{
   if($company) == '')
   {
      alert('Please enter your Company Name);
      return false;
   }
}
</script>
</head>

 

      <form action="post.php" onSubmit="return checkForm(this)" method="POST">
        <table align=center width="575" border=0 cellspacing=10>
          <tr>
            <td align=right><b>Name of Company</b> </td>
            <td><input type="text" name="company" size="50">
            </td>
          </tr>
          <tr>
            <td align=center colspan="2"><input type="submit" name="Submit" value="Submit Form">
            </td>
          </tr>
</table>

 

Any ideas?

 

Regards,

DED

 

First of all, this is a JavaScript validator you're trying to write, not a PHP one.

 

Secondly, you put "return checkForm(this)" in the HTML of the form, but your function doesn't accept an argument (i.e., it's written as "checkForm(){ /*... */}").

 

Third, where does $company come from?  If it's supposed to be from the PHP script, then, put simply, you're doing it wrong.  JavaScript execution always occurs before the page is reloaded, and the form values are sent to the PHP script.  So, right now, $company = ''

 

If $company is supposed to be supplied by the form itself to the JavaScript, then, again, you're doing it wrong.  You need to grab a hold of that form field, then retrieve its value.

 

Here's the best solution, assuming your validator is JavaScript-only:

<html>
<head>
   <title>blah</title>
   <script type="text/javascript">
      window.onload = function()
      {
         var myForm = document.forms['myForm'];

         myForm.onsubmit = function()
         {
            if(this.elements['company'].value == '')
            {
               alert("Please enter company name");
               return false;
            }
            else
            {
               return true;
            }
         }
      }
   </script>
</head>

<body>
      <form name="myForm" action="post.php" method="POST">
        <table align=center width="575" border=0 cellspacing=10>
          <tr>
            <td align=right><b>Name of Company</b> </td>
            <td><input type="text" name="company" size="50">
            </td>
          </tr>
          <tr>
            <td align=center colspan="2"><input type="submit" name="Submit" value="Submit Form">
            </td>
          </tr>
        </table>
      </form>
</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/140676-form-validation/#findComment-736225
Share on other sites

Nightslyr....thank you for your help with this.  Did forms before, but never validated it. 

 

One more problem maybe you can help me with:

I have this:

 <input type="checkbox" name="iagree" value="Y">
              <left><strong>I Agree</strong></left>

 

and this:

			else if(this.elements['iagree'].value != 'Y')
            {
               alert("Please check the I AGREE box to accept the terms");
               return false;
            }

 

The form is process whether or not the box is checked.

 

Regards,

DED

Link to comment
https://forums.phpfreaks.com/topic/140676-form-validation/#findComment-736281
Share on other sites

A checkbox value is the same whether the checkbox is checked or not. You need to test if the field is checked - not the value:

 

			else if(!this.elements['iagree'].checked)
            {
               alert("Please check the I AGREE box to accept the terms");
               return false;
            }

Link to comment
https://forums.phpfreaks.com/topic/140676-form-validation/#findComment-736322
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.