以下jsp文件不会从单选按钮开始验证

时间:2015-10-26 05:21:00

标签: javascript forms jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Validation Form</title>

<script type="text/javascript">


function validate()
{
	
	var name_str=document.my_form.name.value;
	if((name_str==null)||(name_str==""))
		{
		alert("Enter Name")
		return false
		}
	
	 var pwd_str=document.my_form.pwd.value;
	if((pwd_str=="null")||(pwd_str==""))
		{
		alert("Enter Password")
		return false
		}
	
	var repwd_str=document.my_form.repwd.value;
	if((repwd_str=="null")||(repwd_str==""))
		{
		alert("ReEnter Password")
		return false
		}

	if(pwd_str!=repwd_str)
	{    
		alert("password must be same!");  
		return false  
		}  
	
	var age_str=document.my_form.age.value;
	if((age_str=="null")||(age_str==""))
		{
		alert("enter age")
		return false
		}
	
	if (isNaN(age_str))
	{
		  alert("only numeric")
		  return false
	}
	
	var ph_str=document.my_form.ph.value;
	if((ph_str=="null")||(ph_str==""))
	{
	alert("enter phone number")
	return false
	}

	if (isNaN(ph_str))
	{
		  alert("only numeric ph")
		  return false
	}

	if((ph_str.length<1)||(ph_str.length>10))
		{
		alert("Invalid length of ph")
		return false
		}
	
	var email_str=document.my_form.email.value;
	if((email_str=="null")||(email_str==""))
	{
	alert("enter email")
	return false
	}
	 
	var atposition=email_str.indexOf("@");  
	var dotposition=email_str.lastIndexOf(".");  
	if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length)
	{  
	  alert("Please enter a valid e-mail address")
	  return false 
	} 
	
	if ((!document.getElementById("a").checked)&&(!document.getElementById("b").checked))
		{
        alert("no button is selected");
        return false
	} 
	
	var i;
	var group1 = document.my_form.hobby;
	for (var i=0; i<group1.length; i++) {
	if (group1[i].checked)
	break;
	}

	if (i==group1.length)
	return alert("No box is checked"); 
	
	var group2 = document.getElementById.dd;
	var index_opt = group2.options[group2.selectedIndex].value;
	if(index_opt==Select)
	{
		alert("select course")
		return false
	}
	
}	

</script>


</head>
   
 <body bgcolor=aqua>
   
 <center><h3>Application Form</h3></center>

<form name="my_form" onsubmit="validate()">

<strong>Name:&nbsp&nbsp&nbsp</strong>
<input type=text name=name><br/>

<strong>Password:&nbsp&nbsp&nbsp</strong>
<input type=password name=pwd><br/>
<strong>Retype Password:&nbsp&nbsp&nbsp</strong>
<input type=password name=repwd><br/>

<strong>Age:&nbsp&nbsp&nbsp&nbsp</strong>
<input type=text name=age><br/>

<strong>Phone No:&nbsp&nbsp&nbsp&nbsp</strong>
<input tupe= text name=ph><br/>

<strong>Email:&nbsp&nbsp&nbsp&nbsp</strong>
<input type=text name=email><br/><br/>

<strong>Sex:&nbsp&nbsp&nbsp&nbsp</strong>
<input type= "radio" name="gender" id="a" value="Male">Male&nbsp&nbsp&nbsp&nbsp
<input type= "radio" name="gender" id="b" value="Female">Female&nbsp&nbsp&nbsp&nbsp<br/><br/><br/>

<strong>Hobby:</strong>
<input type="checkbox"name= "hobby" id="1" value="singning">Singning<br/>
<input type="checkbox"name= "hobby" id="2" value="reading">Reading<br/>
<input type="checkbox"name= "hobby" id="3" value="tv">TV<br/>
<br/>

<strong>Country</strong>
<select name="mymenu" id="dd">
<option value ="Select">Select</option>
<option value ="India">India</option>
<option value ="China">China</option>
<option value ="SriLanka">SriLanka</option>
</select>


<input type="submit" value=Submit>
<input type="reset" value=Reset><br/>
</form>

</body>
</html>

代码不会从病房的单选按钮验证。但是,如果我运行单选按钮代码和单选按钮代码后的其他验证代码,它工作,并在单个表单编译时,它不起作用。下拉菜单验证根本不起作用。请帮我。提前谢谢。

2 个答案:

答案 0 :(得分:0)

单选按钮检查不是问题。在您的代码中,您可以使用一个变量,如x,这就是问题所在。请参考下面的代码:

if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length)

上面的代码x变量没有声明,也没有在代码中指定任何位置。这样就发生了线路错误并终止了。

您将此x更改为email_str,根据您的代码应如下所示,

if (atposition<1 || dotposition<atposition+2 || dotposition+2>=email_str.length)

工作正常。

答案 1 :(得分:0)

首先,您应该更改提交事件,如下所示

<form name="my_form" onsubmit="return validate(this.form)">

这里是您完整的Javascript

<script type="text/javascript">
function validate(form)
{
var name_str=document.my_form.name.value;
if((name_str==null)||(name_str==""))
{
    alert("Enter Name")
    return false
    }
var pwd_str=document.my_form.pwd.value;
if((pwd_str=="null")||(pwd_str==""))
    {
    alert("Enter Password")
    return false
    }
var repwd_str=document.my_form.repwd.value;
if((repwd_str=="null")||(repwd_str==""))
    {
    alert("ReEnter Password")
    return false
    }
if(pwd_str!=repwd_str)
{    
    alert("password must be same!");  
    return false  
    }  
var age_str=document.my_form.age.value;
if((age_str=="null")||(age_str==""))
    {
    alert("enter age")
    return false
    }
if (isNaN(age_str))
{
      alert("only numeric")
      return false
}
var ph_str=document.my_form.ph.value;
if((ph_str=="null")||(ph_str==""))
{
alert("enter phone number")
return false
}
if (isNaN(ph_str))
{
      alert("only numeric ph")
      return false
}
if((ph_str.length<1)||(ph_str.length>10))
    {
    alert("Invalid length of ph")
    return false
    }
var email_str=document.my_form.email.value;
if((email_str=="null")||(email_str==""))
{
alert("enter email")
return false
}
var email = document.getElementById('mail');
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
var gender=document.my_form.gender.value;
if((gender=="null")||(gender=="")){
   alert("Please Select Gender");
   return false;
}
var i;
var group1 = document.my_form.hobby;
for (var i=0; i<group1.length; i++) {
if (group1[i].checked)
break;
}
if (i==group1.length){
    alert("No box is checked"); 
    return false;
}
var country=document.my_form.mymenu.value;
if(country=='Select'){
   alert("You must Select your Country");
   return false;
}
}   
</script>