使用javascript进行PHP表单验证

使用javascript进行PHP表单验证,php,javascript,html,Php,Javascript,Html,我正在使用php和mysql开发一个应用程序。我已经编写了一个用于表单验证的javascript代码,但它可以正常工作 function checkForm() { if(document.Form1.Gestational_age.value=="") { alert('Please select an option for Gestational age in weeks ') ;

我正在使用php和mysql开发一个应用程序。我已经编写了一个用于表单验证的javascript代码,但它可以正常工作

     function checkForm()
        { if(document.Form1.Gestational_age.value=="")
                {
                   alert('Please select an option for Gestational age in weeks ') ;
                   return false;
                }
            else if(document.Form1.PRBC.value=="")
                {
                    alert('Please select an option for PRBC transfusion ');
                    return false;
                }
            else if(document.Form1.milk_feeding.value=="")
                {
                    alert('Please select an option for Human milk feeding at both day 7     and day 14 of life');
                    return false;
                }
    }
   </script>

    <title>GutCheckNEC for infants < 1500 grams at birth Gephart,2012</title>

   </head>
   <body>

    <div><b>GutCheckNEC   for infants < 1500 grams at birth Gephart, 2012</b></div>
    <br>
    <form name="Form1" method="post" action ="Form1.php" onSubmit="return checkForm();">
        1.&nbsp;&nbsp;&nbsp Gestational age in weeks&nbsp;&nbsp;&nbsp
         <input type="radio" id="Gestational_age" name="Gestational_age" value="0-27"/>0-27
         <input type="radio" name="Gestational_age" value="28-31"/>28-31
         <input type="radio" name="Gestational_age" value=">32" />32
         <br>

        2.&nbsp;&nbsp;&nbsp PRBC transfusion&nbsp;&nbsp;&nbsp
        <input type="radio" id="PRBC" name="PRBC" value="Yes"/>Yes
         <input type="radio" name="PRBC" value="No"/>No
         <br>
        3.&nbsp;&nbsp;&nbsp Human milk feeding at both day 7 and day 14 of life&nbsp;&nbsp;&nbsp
        <input type="radio" id="milk_feeding" name="milk_feeding" value="Yes"/>Yes
         <input type="radio" name="milk_feeding" value="No"/>No
         <br>

     <input type="submit" name="submit" value="Next"/>
    </form>
    </body>
   </html>
函数检查表()

{if(document.Form1.胎龄值==“”) { 警报(“请选择一个以周为单位的胎龄选项”); 返回false; } else if(document.Form1.PRBC.value==“”) { 警报(“请为PRBC输血选择一个选项”); 返回false; } else if(document.Form1.milk\u feeding.value==“”) { 警报(“请选择在生命第7天和第14天喂养母乳的选项”); 返回false; } } 适用于出生时小于1500克的婴儿的GutCheckNEC Gephart,2012年 适用于出生时小于1500克的婴儿的GutCheckNEC Gephart,2012年
1.  孕周数 0-27 28-31 32
2.   PRBC输血 对 不
3.  在出生后第7天和第14天喂食母乳 对 不

这是我的代码示例。你能告诉我,为什么我的javascript部分不工作吗?

你不能像这样获得单选按钮的值:

document.Form1.Gestational_age.value
像这样使用它

var ages = document.getElementsByName('Gestational_age');
var ageIsChecked = false;
for (var i = 0, length = ages.length; i < length; i++) {
    if (ages[i].checked) {
        ageIsChecked = true;
    }
}
if (!ageIsChecked) {
    alert('Please select an option for Gestational age in weeks ');
    return false;
}
var ages=document.getElementsByName(“孕龄”);
var ageIsChecked=false;
对于(变量i=0,长度=ages.length;i
您不能像这样获取单选按钮的值:

document.Form1.Gestational_age.value
像这样使用它

var ages = document.getElementsByName('Gestational_age');
var ageIsChecked = false;
for (var i = 0, length = ages.length; i < length; i++) {
    if (ages[i].checked) {
        ageIsChecked = true;
    }
}
if (!ageIsChecked) {
    alert('Please select an option for Gestational age in weeks ');
    return false;
}
var ages=document.getElementsByName(“孕龄”);
var ageIsChecked=false;
对于(变量i=0,长度=ages.length;i
试试看,
胎龄
是带有数组的名称,因此您需要传递为
[“Form1”][“胎龄”]
checked
属性以检查单选按钮是否选中的单选按钮长度

function checkForm()
{ 
    if(document.forms["Form1"]["Gestational_age"].checked == "")
    {
        alert('Please select an option for Gestational age in weeks ') ;
        return false;
    }
    else if(document.forms["Form1"]["PRBC"].checked == "")
    {
        alert('Please select an option for PRBC transfusion ');
        return false;
    }
    else if(document.forms["Form1"]["milk_feeding"].checked == "")
    {
        alert('Please select an option for Human milk feeding at both day 7     and day 14 of life');
        return false;
    }
}

它会根据您的需要提醒您进行验证,因为我已经进行了测试。

尝试,
胎龄
是带有数组的名称,因此您需要作为
[“Form1”][“胎龄”]
checked
属性来检查单选按钮是否选中的单选按钮长度

function checkForm()
{ 
    if(document.forms["Form1"]["Gestational_age"].checked == "")
    {
        alert('Please select an option for Gestational age in weeks ') ;
        return false;
    }
    else if(document.forms["Form1"]["PRBC"].checked == "")
    {
        alert('Please select an option for PRBC transfusion ');
        return false;
    }
    else if(document.forms["Form1"]["milk_feeding"].checked == "")
    {
        alert('Please select an option for Human milk feeding at both day 7     and day 14 of life');
        return false;
    }
}

它将根据您的需要提醒您的验证,因为我已经测试过了。用此函数替换验证函数,然后重试。这将检查该值是否已设置

    function checkForm()
    { 
        if(!document.forms["Form1"]["Gestational_age"][0].checked && !document.forms["Form1"]["Gestational_age"][1].checked && !document.forms["Form1"]["Gestational_age"][2].checked)
            {
               alert('Please select an option for Gestational age in weeks ') ;
               return false;
            }
        else if(!document.forms["Form1"]["PRBC"][0].checked && !document.forms["Form1"]["PRBC"][1].checked)
            {
                alert('Please select an option for PRBC transfusion ');
                return false;
            }
        else if(!document.forms["Form1"]["milk_feeding"][0].checked && !document.forms["Form1"]["milk_feeding"][1].checked)
            {
                alert('Please select an option for Human milk feeding at both day 7 and day 14 of life');
                return false;
            }
    }

用此函数替换验证函数,然后重试。这将检查该值是否已设置

    function checkForm()
    { 
        if(!document.forms["Form1"]["Gestational_age"][0].checked && !document.forms["Form1"]["Gestational_age"][1].checked && !document.forms["Form1"]["Gestational_age"][2].checked)
            {
               alert('Please select an option for Gestational age in weeks ') ;
               return false;
            }
        else if(!document.forms["Form1"]["PRBC"][0].checked && !document.forms["Form1"]["PRBC"][1].checked)
            {
                alert('Please select an option for PRBC transfusion ');
                return false;
            }
        else if(!document.forms["Form1"]["milk_feeding"][0].checked && !document.forms["Form1"]["milk_feeding"][1].checked)
            {
                alert('Please select an option for Human milk feeding at both day 7 and day 14 of life');
                return false;
            }
    }
这里有一个例子

<script>
function checkForm(){ 
if(Form1.Gestational_age[0].checked==false && Form1.Gestational_age[1].checked==false && Form1.Gestational_age[2].checked==false)
   {
      alert('Please select an option for Gestational age in weeks ') ;
      return false;
    }           
   else {
     return true;   
   }
}
</script>

函数checkForm(){
如果(Form1.胎龄[0]。选中==false&&Form1.胎龄[1]。选中==false&&Form1.胎龄[2]。选中==false)
{
警报(“请选择一个以周为单位的胎龄选项”);
返回false;
}           
否则{
返回true;
}
}
我只做了第一组单选按钮。希望这能给你一个想法。

这里是一个例子

<script>
function checkForm(){ 
if(Form1.Gestational_age[0].checked==false && Form1.Gestational_age[1].checked==false && Form1.Gestational_age[2].checked==false)
   {
      alert('Please select an option for Gestational age in weeks ') ;
      return false;
    }           
   else {
     return true;   
   }
}
</script>

函数checkForm(){
如果(Form1.胎龄[0]。选中==false&&Form1.胎龄[1]。选中==false&&Form1.胎龄[2]。选中==false)
{
警报(“请选择一个以周为单位的胎龄选项”);
返回false;
}           
否则{
返回true;
}
}


我只做了第一组单选按钮。希望这能给你一个想法。

什么不起作用?您是否有任何错误消息?没有错误消息,但它没有验证数据,如果我将字段留空,则它不会发出警报消息。什么不起作用?您是否有任何错误消息?没有错误消息,但它没有验证数据,如果我将字段保留为空,则它不会发出警告消息。document.Form1.Gestational_-age[0]。选中仅适用于放射科的第一项。这是我的错,我犯了错误。document.Form1.Gestational_-age[0].checked仅适用于放射学家的第一项。这是我的错,我犯了错误。document.forms[“Form1”][“孕龄”]。值返回为undefined@Arjun感谢您的回复。它正在验证发件人,但即使我正在设置值,它也会显示一个警报框。。(验证不正确)@user1495220抱歉,我的错误。需要检查每个单选按钮。现在可以了。document.forms[“Form1”][“孕龄”]。值返回为undefined@Arjun感谢您的回复。它正在验证发件人,但即使我正在设置值,它也会显示一个警报框。。(验证不正确)@user1495220抱歉,我的错误。需要检查每个单选按钮。现在就可以了。谢谢你的回复。但是我的要求是检查收音机是否被选中,如果没有,我就必须显示警报…刚刚更改的代码,你应该为其他人选择上面代码中的值,或者使用jquery,很容易用一行检查值:$('input[@name=“孕龄”]:checked')。val();感谢您的回复。但我的要求是检查收音机是否已检查,如果没有,我必须显示警报…刚刚更改的代码,您应该为其他人广播上述代码中的值,或者使用jquery,只需一行即可轻松检查值:$('input[@name=“孕龄”]:checked')。val();@罗杰:谢谢,这很有效……)我很高兴我能帮助你。如果这就是你想要的,你可以投票给我的答案。@Roger谢谢,它有效..。)我很高兴我能帮助你。如果这就是你想要的,你可以投票支持我的答案。