简体   繁体   English

Javascript表单验证 - 如何在单个表中显示验证错误而不是使用警报

[英]Javascript form validating - how do you show validation errors in a single table rather than using alerts

I am new to .asp and Javascript and wondered if someone was able to advise how to show validation errors for fields below the form in a single table or text field rather than using alerts. 我是.asp和Javascript的新手,想知道是否有人能够建议如何在单个表或文本字段中显示表单下方字段的验证错误,而不是使用警报。 Alerts and validation are working. 警报和验证正在进行中。

Here is the table that i plan to put the validation errors into: 这是我计划将验证错误放入的表:

 <asp:Table ID="StatusTable" runat="server" Width="100%"><asp:TableRow ID="StatusRow" runat="server"> <asp:TableCell ID="StatusTextCell" runat="server" Width="95%"> <asp:TextBox ID="StatusTextBox" runat="server" Width="100%" /> </asp:TableCell><asp:TableCell ID="StatusPadCell" runat="server" Width="5%"> &nbsp; </asp:TableCell></asp:TableRow></asp:Table> 

And here is an example of some the Javascript I am using where i need the validation error messages to show in the table rather than from alerts. 这里有一些我正在使用的Javascript示例,我需要在表中显示验证错误消息而不是警报。

Would be extremely grateful for any advise 对任何建议都会非常感激

 < script type = "text/javascript" language = "Javascript" > function RequiredTextValidate() { //check all required fields from Completed Table are returned if (document.getElementById("<%=CompletedByTextBox.ClientID%>").value == "") { alert("Completed by field cannot be blank"); return false; } if (document.getElementById("<%=CompletedExtTextBox.ClientID %>").value == "") { alert("Completed By Extension field cannot be blank"); return false; } if (document.getElementById("<%=EmployeeNoTextBox.ClientID %>").value == "") { alert("Employee No field cannot be blank"); return false; } if (document.getElementById("<%=EmployeeNameTextBox.ClientID %>").value == "") { alert("Employee Name field cannot be blank"); return false; } return true; } < /script> 

 function ValidateFields() { //Validate all Required Fields on Form if (RequiredTextValidate() && CheckDate(document.getElementById("<%=SickDateTextBox.ClientID%>").value) && CheckTime(this) && ReasonAbsent() && ReturnDateChanged() && FirstDateChanged() && ActualDateChanged() == true) { return true; } else return false; } 

There are many ways that this type of thing can be done, here is one way which I have put together based on what you have so far... 有很多方法可以做这种事情,这是我根据你到目前为止所做的一种方式...

http://jsfiddle.net/c0nh2rke/ http://jsfiddle.net/c0nh2rke/

 function RequiredTextValidate() { var valMessage = ''; var valid = true; //check all required fields from Completed Table are returned if (document.getElementById("test1").value == "") { valMessage += 'Completed by field cannot be blank <br />'; valid = false; } if (document.getElementById("test2").value == "") { valMessage += 'Completed By Extension field cannot be blank <br />'; valid = false; } if (document.getElementById("test3").value == "") { valMessage += 'Employee No field cannot be blank <br />'; valid = false; } if (document.getElementById("test4").value == "") { valMessage += 'Employee Name field cannot be blank <br />'; valid = false; } if (valid) { return true; } else { document.getElementById("errors").innerHTML = valMessage; return false; } } 
 <form > <input id="test1" type="text" /><br /> <input id="test2" type="text" /><br /> <input id="test3" type="text" /><br /> <input id="test4" type="text" /><br /> <input type="button" value="Submit" onclick="RequiredTextValidate()" /> </form> <div id="errors"><div> 

HTML HTML

<form >
<input id="test1" type="text" /><br />
<input id="test2" type="text" /><br />
<input id="test3" type="text" /><br />
<input id="test4" type="text" /><br />
    <input type="button" value="Submit" onclick="RequiredTextValidate()" />
</form>
<div id="errors"><div>

Script 脚本

function RequiredTextValidate() {
    var valMessage = '';
    var valid = true;
    //check all required fields from Completed Table are returned
    if (document.getElementById("test1").value == "") {
        valMessage += 'Completed by field cannot be blank <br />';
      valid = false;
    }

    if (document.getElementById("test2").value == "") {
        valMessage += 'Completed By Extension field cannot be blank <br />';
      valid = false;
    }

    if (document.getElementById("test3").value == "") {
        valMessage += 'Employee No field cannot be blank <br />';
      valid = false;
    }

    if (document.getElementById("test4").value == "") {
        valMessage += 'Employee Name field cannot be blank <br />';
      valid = false;

    }
    if (valid) {
    return true;
    }
    else
    {
        document.getElementById("errors").innerHTML = valMessage;
        return false;
    }
  }

The best JavaScript is no JavaScript. 最好的JavaScript不是JavaScript。

To that end, consider: 为此,请考虑:

 <form action="javascript:alert('Submitted!');" method="post"> <input type="text" name="someinput" placeholder="Type something!" required /> <input type="submit" value="Submit form" /> </form> 

Notice how the browser will render native UI to show that the field was left blank? 请注意浏览器如何呈现本机UI以显示该字段是空白的? This is by far the best way to do it because it doesn't rely on JavaScript. 这是迄今为止最好的方法,因为它不依赖于JavaScript。 Not that there's anything wrong with JS, mind, but far too often a single typo in form validation code has caused hours of debugging! 并不是说JS存在任何问题,但是表单验证代码中的一个错误通常会导致数小时的调试!

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

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