简体   繁体   English

如何运行此javascript代码以验证html文档中的表单?

[英]How do I run this javascript code to validate my form in my html doc?

<!DOCTYPE html>
<head></head>
<body>
<script>
            function validateForm (){
                var formPi = 0
                var formPi = document.forms["Personal Information"]["firstname"].value;
                if(formPi == null ){
                    alert("Please fill out your First Name!");
                    return false;
                }

            }
</script>
<form name="Personal Information" action="" >
          <fieldset>
            <legend>Personal information:</legend>
            <br>
            First name:<br>
            <input type="text" name="firstname" value="" >
            <br>
            Last name:<br>
            <input type="text" name="lastname" value="" >
            <br>
            Email Address:<br>
            <input type="text" name="Email Address" value="" >
            <br>
            <br><br>
            <input type="submit" value="Submit" onsubmit="return validateForm()">
            <!-- The for is meant to be validated once the subit button has been clicked-->
          </fieldset>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>

<form action="demo_form.asp" method="post">
  <input type="text" name="fname" required>

   </form>      
  <p>If you click submit, without filling out the text field,
  your browser will display an error message.</p>

</body>
</html>

U can use required label for validations in each input tag U可以在每个input tag使用required标签进行验证

add the onsubmit attribute to form tag 将onsubmit属性添加到表单标签

onsubmit="return validateForm();"

Update function validateForm to return true or false based on whether validation succeeds or fails. 更新函数validateForm以根据验证是否成功来返回true或false。 For more information see: https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit 有关更多信息,请参见: https : //developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit

Move the onsubmit="return validateForm()" from your submit button to the form element, where it belongs. onsubmit="return validateForm()"submit按钮移动到它所属的form元素。 You also may change it to return validateForm(this) : 您也可以将其更改为return validateForm(this)

<form name="Personal Information" action="" onsubmit="return validateForm(this)" >

Your javascript function is almost ok, just add a return true; 您的javascript函数几乎可以,只需添加return true; at the end, get the form from the this parameter we've added above and change the if condition: 最后,从上面添加的this参数中获取表格,并更改if条件:

function validateForm (form) {
    var formPi = form["firstname"].value;

    if (formPi == null || formPi == "") { // <-- condition changed
        alert("Please fill out your First Name!");
        return false;
    }

    return true;
 }

Form demo 表格演示

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

相关问题 如何在cakephp中使用javascript验证我的html表单控件? - How do I validate my html form controls using javascript in cakephp? 如何使用JavaScript修复错误的HTML表单? - How do I fix my faulty HTML form with JavaScript? 如何将我的 html/javascript 代码保存为程序? - How do I save my html/javascript code as a program? 如何将我的 html 代码连接到 javascript 中的控制器和路由? - How do I connect my html code to controllers and routes in javascript? 如何在 html div 的代码中显示 javascript 计时器? - How do I show the javascript timer in my code in an html div? 如何让我的 html 电子邮件表单在发送到 php 脚本进行发送之前使用 javascript 进行验证 - How to I get my html email form to validate using javascript before sending it to the php script for sending 使用JavaScript如何在我的页面中插入HTML代码? - Using JavaScript how do I insert html code into my page? Dom Javascript如何将我的html表单与我的Javascript代码连接起来 - Dom Javascript how can I connect my html form with my Javascript code 如何在不使用警报的情况下使用 Javascript 验证我的表单? - How do I Validate my form using Javascript without using alert? 如何在提交时验证此 HTML/JavaScript 表单? - How do I validate this HTML/JavaScript form onsubmit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM