質問

I've got a little problems with my "contact us" form, whenever i submit it i get the error message 500. IE tells me it's a error at line 62, and that validate_form is not specified.

My code goes like this:.

<form method="POST" action="/cgi-bin/emailer.asp" onsubmit="return validate_form(this); ">

I really don't know anything about .asp, .php, .js etc. so some help would really be needed.

Thanks -Niko

Update:

function validate_Form(form)
{
var x=document.forms["yhteys"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Sähköposti osoite ei ole oikein.");
  return false;
  }
}

that's how the code looks right now, but it still isn't working.

<form name="yhteys" method="POST" action="/cgi-bin/emailer.asp" onsubmit="return validate_form(this);">
  <div style="float:left;">
    Aihe:<b>*</b><br>
        <select name="Aihe" required="required" id="Aihe">
          <option value="Yhteydenotto">Yhteydenotto</option>
          <option value="Arviokäynti">Arviokäynti</option>
          <option value="Esitetilaus">Esitetilaus</option>
          <option value="Esittelyajan varaus">Esittelyajan varaus</option>
          <option value="Palaute">Palaute</option>
          <option value="Muu viesti">Muu viesti</option>
        </select><br><br>
    Nimi:<b>*</b><br>
    <input type="text" required="required" name="nimi" size="35"><br><br>
    Osoite:<b>*</b><br>
    <input type="text" required="required" name="osoite" size="35"><br><br>
    Puhelin:<b>*</b><br>
    <input type="text" required="required" name="puh" size="35"><br><br>
    Sähköposti:<b>*</b><br>
    <input type="text" required="required" name="email" size="35"><br><br>
    Viesti:<b>*</b><br>
    <textarea rows="5" name="viesti" cols="45" required="required" id="Viesti"></textarea>
    <div style=" margin-right: 2px; margin-top: 2px;"><input type="submit" value="Lähetä" name="B1"></div><br />
    <p>Tähdellä merkityt kohdat ovat pakollisia.</p>
    </form>
  </div>

There is the whole form section, just so you could tell me more specifically what's wrong.

役に立ちましたか?

解決

validate_form(this) is a call to a JavaScript function you have to define.

it might look something like this:

function validate_form(form){
    if (form.fieldname.value /* fulfills some condition */)
    {
        //this will abort the submit
        return false;
    }
    //will only get called when the if-statement does not return true
    //this allows the submit to procede
    return true;
};

alternatively you can declare a function like this:

var validate_form = function(form){/*your code here*/};

you should put that block of code in the <head>-section of your page inside of:

<script type="text/javascript" >
    // your code
</script>

EDIT:
As of your javascript:

If you send this to your function, you do not have to crawl DOM to get your elements.
--> to get any field in your form (which you pass to function with this-Keyword) you can do the following:

form.fieldname

this allows you to access your email like that:

var email = form.email.value;

You can now check your email with a custom-validation, but i recommend using a freely available regex to check it. you can find a nice one in the answer no.3 here

if (!isValid(email)){ //if the given email is not Valid by the function you call
alert("Sähköposti osoite ei ole oikein.");
return false;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top