繁体   English   中英

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

[英]javascript empty field form validation not working

嗨,我是javascript新手。

我正在尝试实现一些JavaScript表单验证,我正在尝试使用此处使用的技术: http : //www.w3schools.com/js/tryit.asp?filename=tryjs_form_validation以测试表单中的空字段并使用警报向用户发出警告,但对我来说不起作用当我向jsp文件提交空表单时,javascript无法捕获错误

这是我的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>'

有人看到我在做什么错吗? 谢谢你的时间。

在此代码中将=替换为===

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

了解比较运算符

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 ==用于比较值。

在代码中替换下面的代码块。

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