简体   繁体   English

使用Java脚本进行验证和打印

[英]Validation With Java Script and printing

I have a form in HTML and that if the fields are left blank, the Javascript will print inside the fields error. 我有一个HTML表单,如果字段留空,则Javascript将在字段错误内打印。 Please can some one give me a piece of code that will validate the form and then will print Error on top of the form if its left blank and not inside the fields of the form? 请有人可以给我一段代码来验证表单,然后在表单顶部空白而不是不在表单域内的情况下将Error打印在表单顶部吗?

My Form: 我的表格:

<form id="contact" onsubmit="checkContactForm(); return false;" onreset="resetForm();">
<p>Fill in the form below to send me a message!</p>    

<div id="errormessage"></div>

<p>
<label for="">  </label>
<input type="text" name="" id="" onfocus="" />

<p>
  <label for="name">Name:</label>
  <input type="text" name="name" id="name" onfocus="resetField(this);" />
</p>
<p>
  <label for="email">E-mail address:</label>
  <input type="text" name="email" id="email" onfocus="resetField(this);" />
</p>
<p>
  <label for="message">Your Message:</label>
  <textarea name="message" id="message" rows="5" cols="25" onfocus="resetField(this);"></textarea>
</p>
<p>
  <button type="submit">Send Message</button>
  <button type="reset">Reset Form</button>
</p>

My Javascript: 我的Javascript:

var requiredFields = ["name", "email", "message"];

function checkContactForm() {
    var myForm = document.forms[0];
    for (i in requiredFields) {
        fieldName = requiredFields[i];
        if (!myForm[fieldName].value || myForm[fieldName].value == "Error") {
            myForm[fieldName].style.color = "#f66";
            myForm[fieldName].value = "";
            var emptyFields = true;
        }
    }

    if (!emptyFields) { myForm.submit(); }
}

function resetField(myField) {
    if (myField.value == "Error") {
        myField.style.color = "#000";
        myField.value = "";
    }
}

function resetForm(myForm) {
    var myForm = document.forms[0];
    for (i in requiredFields) {
        fieldName = requiredFields[i];
        myForm[fieldName].style.color = "#000";
    }
}

Since HTML5 there is a form-validation API ( http://www.w3schools.com/js/js_form_validation.asp ) 从HTML5开始,有一个表单验证API( http://www.w3schools.com/js/js_form_validation.asp

Here you can find a pretty good "tutorial": http://www.smashingmagazine.com/2009/07/07/web-form-validation-best-practices-and-tutorials/ 在这里您可以找到一个很好的“教程”: http : //www.smashingmagazine.com/2009/07/07/web-form-validation-best-practices-and-tutorials/

If I've understand you want the error will be printed in the #errormessage div, right? 如果我了解您的要求,错误将显示在#errormessage div中,对吗? If so you can simply do something like this: 如果是这样,您可以简单地执行以下操作:

function addError(str){
    document.getElementById("errormessage").innnerHTML = document.getElementById("errormessage").innerHTML + str + "<br>";
}

function checkContactForm() {
    var myForm = document.forms[0];
    for (i in requiredFields) {
        fieldName = requiredFields[i];
        if (!myForm[fieldName].value || myForm[fieldName].value == "Error") {
            myForm[fieldName].style.color = "#f66";
            myForm[fieldName].value = "";
            var emptyFields = true;
            addError(fiedlName+" is empty!");
        }
    }

    if (!emptyFields) { myForm.submit(); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM