简体   繁体   English

Java脚本表单验证

[英]Java Script form validation

so what I try is to show an error "First name must be filled out" as a text called "check", not as an alert... Please help:) 所以我想做的是显示一个错误“必须填写名字”作为名为“ check”的文本,而不是作为警报...请帮助:)

  <html>
  <body>

 <form name="myForm" action="page.html" onsubmit="return validateForm()" method="post">
  First name: <input type="text" name="box>
 <input type="submit" value="Submit">
</form> 
<p id="check"></p>


<script>

function validateForm() {
var x = document.forms["myForm"]["box"].value;
if (x == null || x == "") {
    alert("First name must be filled out") ="check"; // I tried to send alert as text... 
    return false;
 }
 } 

  </script>

  </body>
   </html> 

Thanks in advance! 提前致谢!

Change 更改

alert("First name must be filled out") ="check";

to

document.getElementById('check').innerHTML = 'First name must be filled out';

As pure Javascript Solution you could replace the alert with the following code: 作为纯Javascript解决方案,您可以使用以下代码替换alert

document.getElementById('check').innerHTML = "First name must be filled out";


In jQuery you could try: jQuery中,您可以尝试:

$( "#check" ).html( 'First name must be filled out' );

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

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