2013-03-09 165 views
1

我正在开发一个应用程序,使用php和mysql.I已经为表单验证写了一个javascript代码,但它的工作。使用javascript进行PHP表单验证

 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> 

这是我code.Could的样品请你HEL,为什么我的JavaScript部分没有工作..

+0

什么不起作用?你有任何错误消息? – j0k 2013-03-09 11:10:54

+0

没有错误消息,但它没有验证数据,如果我将这些字段留空,那么它不会给出警报消息。 – user1495220 2013-03-09 11:12:24

回答

1

你不能得到单选按钮的值是这样的:

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; 
} 
+0

感谢您的答复..但我的要求是检查是否收到一个电台nd incase不是然后我必须显示警报... – user1495220 2013-03-09 11:41:07

+0

只是改变了代码,你应该为其他人的上述代码广播值,或使用jQuery ,它很容易检查一行的值:$('input [@ name =“Gestational_age”]:checked')。val(); – sanj 2013-03-09 11:47:27

0

尝试,Gestational_age是名与数组,所以你需要通过为["Form1"]["Gestational_age"]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; 
    } 
} 

,只要你想,因为我已经测试过它会提醒您的验证。

+0

document.Form1.Gestational_age [0] .checked只适用于第一项radiolist – sanj 2013-03-09 11:30:31

+0

雅这是我的错,我犯了错误。 – 2013-03-09 11:35:13

2

用此替换验证函数并尝试。这将检查值是否已设置。

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; 
      } 
    } 
+0

document.forms [“Form1”] [“Gestational_age”]。value返回为undefined – sanj 2013-03-09 11:32:59

+0

@Arjun谢谢你的回复..它正在验证from,但它显示一个警告框,即使im设置值..(它没有正确验证) – user1495220 2013-03-09 11:35:34

+0

@ user1495220对不起,我的坏。需要检查每个单选按钮。它现在会工作。 – 2013-03-09 11:45:33

2

下面是一个例子

<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> 

我这样做,是只有第一组单选按钮。希望这给你一个想法。

+0

@罗杰谢谢,它的工作.. :) – user1495220 2013-03-09 11:44:23

+1

我很高兴我能够帮助你。如果多数民众赞成你所寻找的,你可以投票我的答案。 – Roger 2013-03-09 11:51:02