Jump to content

[SOLVED] need help with form validation, possibly needing an action listener


Jago6060

Recommended Posts

I have a form that need to validate it's fields, and then use the server's cgimail.  The problem I'm having is that my formIsValid() function alerts when something is wrong but the script continues to send the fields.  I thought that maybe I could set it up to show the submit button once all the required fields were filled.  If that's the best way to do this, any help in doing it would be appreicated, otherwise, I'm open to ther solutions.  Here is the stuff involved.

 

function formIsValid()
{
var email = document.inquiry.email.value;
var comment = document.inquiry.note.value;
var name = document.inquiry.name.value;

<!-- code borrowed from phpfreaks member stockton -->
var bError = false;
var strErrorMsg = "";
if (name == "")
    {
    strErrorMsg += "You must enter a name\n";
    bError = true;
    }
else
if ((email == "") || (email.indexOf(' ')==-1 && 0<email.indexOf('@') && email.indexOf('@')+1 < email.length))
    {
    strErrorMsg += "You must enter a valid email address\n";
    bError = true;
    }
else
if (comment == "")
    {
    strErrorMsg += "Please enter a comment or question\n";
    bError = true;
    }
if (bError == true)
    {
    alert(strErrorMsg);
    }
}

 

The form...

 

<form name=inquiry action="/cgi-bin/cgiemail" onSubmit = "return formIsValid()" method="post">
<table border = 0 cellpadding = 3>
<tr><td align = right>
<font face = Comic Sans MS color = #b7b7b7>*Name:</td>
<td><input name = name size = 60></td>
</tr><tr><td align = right>
<font face = Comic Sans MS color = #b7b7b7>*E-mail Address:</td>
<td><input name = email size = 60></td>
</tr><tr><td align = right>
<font face = Comic Sans MS color = #b7b7b7>Phone Number:</td>
<td><input name = phone size = 60></td>
</tr><tr><td align = right>
<font face = Comic Sans MS color = #b7b7b7>*Question/Comment Topic</td>
<td>
<select name = re>
<option>Product Information</option>
<option>Installation Question</option>
<option>Car Audio</option>
<option>Accessories</option>
<option>Bedliners</option>
<option>Other</option>
</select>
</td>
</tr><tr><td align = right>
<font face = Comic Sans MS color = #b7b7b7>*Comments:</td>
<td colspan = 2 align = center>
<textarea rows = 10 cols = 46 name = note></textarea>
<br>
<input type = submit value = "Contact Us">
</td></tr></table></form>

 

P.S. I didn't write the form, I'm helping a friend, so please ignore the coding format, thanks.

Link to comment
Share on other sites

Two things need to be done:

1.)

Change all the true to false and false to true.

So that when ur function will return false when invalid and true if valid.

 

2.)

add this to the end of ur function statement

 

return bError;

 

To return the status.

Link to comment
Share on other sites

Two things need to be done:

1.)

Change all the true to false and false to true.

So that when ur function will return false when invalid and true if valid.

 

2.)

add this to the end of ur function statement

 

return bError;

 

To return the status.

 

This look better?

 

function formIsValid()
{
var email = document.inquiry.email.value;
var comment = document.inquiry.note.value;
var name = document.inquiry.name.value;

<!-- code borrowed from phpfreaks member stockton -->
var bError = true;
var strErrorMsg = "";
if (name == "")
    {
    strErrorMsg += "You must enter a name\n";
    bError = false;
    }
else
if (email == "")
    {
    strErrorMsg += "You must enter a valid email address\n";
    bError = false;
    }
else
if (comment == "")
    {
    strErrorMsg += "Please enter a comment or question\n";
    bError = false;
    }
if (bError == false)
    {
    alert(strErrorMsg);
    }
return bError;
}

Link to comment
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.