使用 javascript 的 PHP 表单验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15309980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP form validation using javascript
提问by user1495220
I am developing an application in using php and mysql.I have written a javascript code for form validation but its working .
我正在使用 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.   Gestational age in weeks  
<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.   PRBC transfusion  
<input type="radio" id="PRBC" name="PRBC" value="Yes"/>Yes
<input type="radio" name="PRBC" value="No"/>No
<br>
3.   Human milk feeding at both day 7 and day 14 of life  
<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>
This s the sample of my code.Could you please hel,why my javascript part is not working..
这是我的代码示例。请您帮忙,为什么我的 javascript 部分不起作用..
回答by Arjun Abhynav
Replace the validation function by this and try. This checks if the value has been set or not.
用这个替换验证功能并尝试。这将检查该值是否已设置。
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;
}
}
回答by Roger
Here is an Example
这是一个例子
<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>
I have done that for only the first set of Radio Button. Hope this gives you an idea.
我只为第一组单选按钮完成了这项工作。希望这能给你一个想法。
回答by sanj
You can't get value of radio button like this:
你不能像这样获得单选按钮的价值:
document.Form1.Gestational_age.value
use it like this
像这样使用它
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;
}
回答by Devang Rathod
Try , Gestational_age
is name with array so you need to pass as ["Form1"]["Gestational_age"]
and also checked
attribute to check radio button length that radio button is checked or not.
Try ,Gestational_age
是带有数组的名称,因此您需要传递 as["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;
}
}
It will alert your validation as you want because i have tested.
它会根据您的需要提醒您的验证,因为我已经测试过。