表单验证不与表单操作

时间:2017-01-18 18:00:26

标签: javascript php html phpmyadmin

我有一个注册表单,应该根据我创建的每个函数(在validation.js文件中)检查输入数据,在一切正常之后,它应该将所有数据返回到.php文件,这将保存在phpmyadmin中数据库。

然而它无效

Test1:验证工作正常(单独不包括action = .php) Test2:未经验证,数据存储在数据库中(phpmyadmin) 测试3:通过验证并使用action = .php)它无法使用验证部分

请帮帮我。



function formValidation()
{
	var id = document.registration.nic;
	var registerno = document.registration.regno;
	var name = document.registration.pname;
	var paddress = document.registration.address;
    var maritial = document.registration.mars;
    var religion = document.registration.rel;
	var addname = document.registration.fhn;
    var pcity = document.registration.city;
	var telephone = document.registration.tel;
	var age = document.registration.ag;
    var gender = document.registration.gen;
    
	
	
		
	 
	if(ValidateNIC(id)) //done
		{
		if(allLettername(name)) //done
			{
			
				
				 if(allnumericreg(registerno))
					{
					if(alphanumericpaddress(paddress)) //done
						{
						if(maritialselect(maritial)) //done
							{
							if(religionselect(religion)) //done
								{
								 
									
									if(allLetteraddname(addname)) //done
										{
										if(alphanumericpcity(pcity)) //done
											{
											if(allnumerictel(telephone)) //done
												{
												if(allnumericage(age)) //done
													{
													if(genderselect(gender)) //done
														{
															
														}
													}
												}
											}
    									}
									
								}	
							}	
						}
					}
					
					
			}
		}
		return false;
}




function allLettername(name)
{
    var letters = /^[A-Za-z ]+$/;
    if(name.value.match(letters))
    {
        return true;
    }
    else
    {
        alert('Patient Name must have alphabet characters only');
        return false;
    }
}

function allLetteraddname(addname)
{
    var letters = /^[A-Za-z ]+$/;
    if(addname.value.match(letters))
    {
        return true;
    }
    else
    {
        alert('Father or Husband"s Name must have alphabet characters only');
        return false;
    }
}

function maritialselect(maritial)
{
    if(maritial.value == "Default")
    {
        alert("Select your Maritial Status from the list");
        return false;
    }
    else
    {
        return true;
    }
}

 function ValidateNIC(id)
  {
      var letters = /^[0-9a-zA-Z ]+$/;

    if ((id.value.length == 10) && id.value.match(letters))
		{
		return true;
		}
	else
		{
		alert("NIC must contain alphanumeric characters only and must be 10 charators long!");
		id.focus();
		return false;
		}
   }
  

function alphanumericpaddress(paddress)
{
    var letters = /^[0-9a-zA-Z ]+$/;
    if(paddress.value.match(letters))
    {
        return true;
    }
    else
    {
        alert('Patient address must have alphanumeric characters only');
        paddress.focus();
        return false;
    }
}

function alphanumericpcity(pcity)
{
    var letters = /^[0-9a-zA-Z ]+$/;
    if(pcity.value.match(letters))
    {
        return true;
    }
    else
    {
        alert('User City must have alphanumeric characters only');
        pcity.focus();
        return false;
    }
}

function religionselect(religion)
{
    if(religion.value == "Default")
    {
        alert("Select your Religion from the list");
        return false;
    }
    else
    {
        return true;
    }
}

function allnumericreg(registerno)
{
    var numbers = /^[0-9]+$/;
    if(registerno.value.match(numbers))
    {
        return true;
    }
    else
    {
        alert("Register No Must have numeric characters only");
        return false;
    }
}

function allnumerictel(telephone)
{
    var numbers = /^[0-9]+$/;
    if((telephone.value.length == 10) && telephone.value.match(numbers))
    {
        return true;
    }
    else
    {
        alert("Telephone Number must have numeric characters only and Length should 10 charators long! ");
        return false;
    }
}

function allnumericage(age)
{
    var numbers = /^[0-9]+$/;
    if(age.value.match(numbers))
    {
        return true;
    }
    else
    {
        alert("Age must have numeric characters only");
        return false;
    }
}

function genderselect(gender)
{
    if(gender.value == "Default")
    {
        alert("Select your Gender from the list");
        return false;
    }
    
    else
    {
        alert('Registration Form Successfully Submitted');
        window.location.reload()
        return true;
	}
}

<?php
$con= mysqli_connect ("localhost","root","","hmsjason");
if (mysqli_connect_error()){
echo"Failed to Connect";
}
$sql="INSERT INTO patient(P_NIC,Reg_No,Full_Name,Address,Maritial_Sts,Religion,DOB,H_F_Name,City,Contact,Age,Gender,Room_No)VALUES('$_POST[nic]','$_POST[regno]','$_POST[pname]','$_POST[address]','$_POST[mars]','$_POST[rel]','$_POST[dob]','$_POST[fhn]','$_POST[city]','$_POST[tel]','$_POST[ag]','$_POST[gen]','$_POST[room]')";
if(!mysqli_query($con,$sql)){
die("Error".mysqli_error($con));
}
mysqli_close($con);
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
header("location:reception.html");


?>
&#13;
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="style1.css">
<script src="regvalidation.js"></script>
</head>
<body>
<br><br><br>

<table background="Images/formbg1.jpg" border="2" align="center" width ="825" height="820" >

<th align=center valign=center>

<form action="patientdetails.php" method="POST"/>
<!--form name='registration' onSubmit="return formValidation()"/>-->
<div class="textcenter1">
	<h2>Patient Registration Form</h2>
</div>
<br><br>
<table border="0"; align="center" width ="655" height="550" >
<tr>
<td>
NATIONAL ID :</td><td><input type="text" name="nic" size="40" />
</td>
</tr>
<tr>
<td>
<b>
REGISTRATION NUMBER :</td><td><input type="text" name="regno" size="40" />
</td>
</tr>
<tr>
<td>
PATIENT NAME :</td><td><input type="text" name="pname" size="40" />
</td>
<tr>
<td>
PATIENT ADDRESS :</td><td><input type="text" name="address" size="40" />
</td>
</tr>
<tr>
<td>
Maritial Status: :</td>
			<td>
			<select name="mars">
                <option selected="" value="Default">(Please select your Status)</option>
                <option value="Maried">Married</option>
                <option value="Unmarried">UnMarried</option>
            </select>
			</td>
</tr>
<tr>
<td>
RELIGION :</td>
			<td>
			<select name="rel">
                <option selected="" value="Default">(Please select your Religion)</option>
                <option value="Buddhist">Buddhist</option>
                <option value="Catholic">Catholic</option>
                <option value="Christian">Christian</option>
                <option value="Hindu">Hindu</option>
                <option value="Muslim">Muslim</option>
				<option value="Others">Others</option>
            </select>
			</td>
</tr>
<tr>
<td>
Date of Birth: </td><td><input type="date" name="dob">
<td>
</tr>
<tr>
<td>
FATHER'S NAME/HUSBAND'S NAME :</td><td><input type="text" name="fhn" size="40" />
</td>
</tr>
<tr>
<td>
CITY :</td><td><input type="text" name="city" size="40" />
</td>
</tr>
<tr>
<td>
TELEPHONE NUMBER/MOBILE NUMBER :</td><td><input type="text" name="tel" size="40" />
</td>
</tr>
<tr>
<td>
AGE :</td><td><input type="text" name="ag" size="40" />
</td>
</tr>
<tr>
<td>
Gender :</td>
			<td>
			<select name="gen">
                <option selected="" value="Default">(Please select your Gender)</option>
                <option value="Male">Male</option>
                <option value="Female">Female</option>
				<option value="Other">Other</option>
            </select>
			</td>
</tr>
<tr>
<td>
Room: </td><td><input type="text" name="room">
<td>
</tr>
</b>
<tr>
<td></td>
<td>
<input align="left" type="image" src="Images/submit.png" alt="Submit" name="submit" value="Submit" width="115" height="57"/>
</form> 
 <!--if submit button and reset button in same <form> will perform the formvalidation which leds the reset buttton also work as a submit button(wont reset anyting)-->
<form><input align="center" type="image" src="Images/reset.png" alt="Reset" name="reset" value="reset" width="115" height="57"/>


</td>
</tr>
</form>
</table>


</th>
</table>
<br><br>

</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您的验证方法似乎总是返回false。如果onSubmit调用的代码返回false,则不会执行该操作。 onclick和其他事件函数也是如此。

一旦你评估了所有条件是有效的,你应该返回true告诉表单它可以接管并做它的事情。