简体   繁体   English

javascript空字段表单验证不起作用

[英]javascript empty field form validation not working

Hi I am new to javascript. 嗨,我是javascript新手。

I am trying to implement some javascript form validation I am trying to use the technique used here: http://www.w3schools.com/js/tryit.asp?filename=tryjs_form_validation to test for empty field in the form and using an alert to warn users But it is not work for me When i submit an empty form proceeds to the jsp file and the javascript does not catch the error 我正在尝试实现一些JavaScript表单验证,我正在尝试使用此处使用的技术: http : //www.w3schools.com/js/tryit.asp?filename=tryjs_form_validation以测试表单中的空字段并使用警报向用户发出警告,但对我来说不起作用当我向jsp文件提交空表单时,javascript无法捕获错误

here is my index.html file where the form is 这是我的index.html文件,其中的表单是

<html>
  <body>
    <head>
    <script>
        function validateForm()
        {
            var stoneField=document.forms["bmiForm"]["stone"].value;
            var poundsField=document.forms["bmiForm"]["pounds"].value;
            var kgsField=document.forms["bmiForm"]["kgs"].value;
            var feetField=document.forms["bmiForm"]["feet"].value;
            var inchesField=document.forms["bmiForm"]["inches"].value;

            if ( stoneField = null || stoneField = "" && poundsField = null || poundsField = "" && kgsField = null || kgsField = "" && feetField = null || feetField = "" && inchesField = null || inchesField = "" )
            {
              alert("Please enter a weight and height");
              return false;
            }
            else
            {

             return true;
            } 
        }
    </script>
</head>
<form name ="bmiForm" action="PrintBMI.jsp" onsubmit="return validateForm()" method=post  style="width:250px;">
    <fieldset>
    <legend>BMI Calculator</legend>
    <h3>Enter your weight</h3>
    Stone <input type = text name = "stone" size = 1 maxlength = 2>
    Pounds <input type = text name = "pounds" size = 1 maxlength = 2>
    <br>
    <strong>OR</strong>
    <br>
    KGs <input type = text name = "kgs" size = 1 maxlength = 3>

    <h3>Enter your height</h3>
    Feet <input type = text name = "feet" size = 1 maxlength = 1>
    Inches <input type = text name = "inches" size = 1 maxlength = 2>
    <br>
    <strong>OR</strong>
    <br>
    Metres <input type = text name = "metres" size = 1 maxlength = 4>
    <p><input type=submit value = "Get BMI">
    </fieldset>
</form>
</body>
</html>'

Does anyone see what I'm doing wrong. 有人看到我在做什么错吗? Thanks for your time. 谢谢你的时间。

Replace = to === in this code: 在此代码中将=替换为===

if ( stoneField = null || stoneField = "" && poundsField = null || poundsField = "" && kgsField = null || kgsField = "" && feetField = null || feetField = "" && inchesField = null || inchesField = "" )

Read about Comparison Operators 了解比较运算符

if ( stoneField = null || stoneField = "" && poundsField = null || poundsField = "" && kgsField = null || kgsField = "" && feetField = null || feetField = "" && inchesField = null || inchesField = "" )
        {
          alert("Please enter a weight and height");
          return false;
        }

Use === inside if, not = 如果不是,请在===内部使用=

USE == for comparing value. USE ==用于比较值。

replace below block of code in your code. 在代码中替换下面的代码块。

if ( stoneField == null || stoneField == "" && poundsField == null || poundsField == "" && kgsField == null || kgsField == "" && feetField == null || feetField == "" && inchesField == null || inchesField == "" )

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

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