我想验证我的表格。所以我创建了my_script.js并使用它的onsubmit事件。
<form id="form1" name ="form1" method="POST" action="index.php" onSubmit="return validatePassword()">
但是脚本不能完全正常工作。匿名,telno1是有效的,但其他则不起作用。
例如,如果用户名或telno1为空,然后单击Submit1按钮,则javascript会在unametxt或telno1txt上写错误。但是,如果password或other为null,然后单击Submit1按钮,则会发布表单。
我不明白为什么两个if语句起作用,而其他则不起作用。
index.php
<!DOCTYPE html>
<html class="lt-ie9 lt-ie8 lt-ie7" lang="en">
<html class="lt-ie9 lt-ie8" lang="en">
<html class="lt-ie9" lang="en">
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>TekSifre Değiştirme Ekranı</title>
<link rel="stylesheet" href="css/style.css">
<!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<script type="text/javascript" src="script/my_script.js"></script>
</head>
<body>
<section class="container">
<div class="login">
<h1>TekŞifre Yenileme Ekranı</h1>
<div>Kullanıcı Tipini Seçiniz:</div>
<input type="radio" onclick="if(this.checked){myFunction()}" name="tip" value="personel"/>Personel
<input type="radio" onclick="if(this.checked){myFunction1()}" name="tip" value="ogrenci"/>Öğrenci
<form id="form1" name ="form1" method="POST" action="index.php" onSubmit="return validatePassword()">
<p><input type="text" name="uname" id="uname" value="" placeholder="Kullanıcı Adınız" style="width:130px;">
<input type="label" id="ogrenci" name="ogrenci" readonly=true value="" style="width:130px;">
<span id="unametxt" name="unametxt" class="required"></span></p>
<p><input type="text" id="telno1" name="telno1" value="" placeholder="Cep : 5xx xxx xx xx" style="width:150px;" maxlength="10" >
<span id="telno1txt" name="telno1txt" class="required"></span></p>
<!-- <p><input type="text" id="telno2" name="telno2" value="" placeholder="xxx xxxx" style="width:180px;" maxlength="7">
<span id="telno2txt" name="telno2txt" class="required"></span></p>-->
<p><input type="password" id="password" name="password" value="" placeholder="Eski Şifrenizi Giriniz">
<span id="passwordtxt" name="passwordtxt" class="required"></span></p>
<p><input type="password" id="newPassword" name="newPassword" value="" placeholder="Yeni Bir Şifre Giriniz">
<span id="newPasswordtxt" name="newPasswordtxt" class="required"></span></p>
<p><input type="password" id="new2Password" name="new2Password" value="" placeholder="Yeni Şifreyi Tekrar Giriniz">
<span id="new2Passwordtxt" name="new2Passwordtxt" class="required"></span></p>
<p><input type="submit" id="Submit1" name="Submit1" value="Değiştir" ></p>
<!--<td><input type="submit" name="Submit1" value="Değiştir" class="button" onclick="validatePassword()"></td> -->
</form>
</div>
</section>
<section class="about">
<p class="about-links">
<a href="http://bim.kastamonu.edu.tr/index.php/tr/menu-tek-sifre-tr.html" target="_blank">Tek Şifre Nedir</a>
</p>
<p class="about-author">
© 2014–2014 <a href="http://bim.kastamonu.edu.tr" target="_blank"></a>Kastamonu Üniversitesi<br>
<a href="http://bim.kastamonu.edu.tr" target="_blank">Bilgi İşlem Daire Başkanlığı
</section>
</body>
</html>
<?php
if (isset($_POST['Submit1'])) {
$uname=$_POST['uname'];
$telno1=$_POST['telno1'];
// $telno2=$_POST['telno2'];
$password=$_POST['password'];
$newPassword=$_POST['newPassword'];
$new2Password=$_POST['new2Password'];
csvyaz($uname,$password,$newPassword);
telefoncsvyaz($uname,$telno1,$telno2);
powershellverify($uname,$password,$newPassword);
$varmi=dosyayikontrolet($uname);
if ($varmi)
{
powershelltelephone();
// $a= powershelltelephone();
// echo "<pre>";print_r($a);exit;
powershellreset();
telefonlarcsv($uname,$telno1,$telno2);
$silmi=dosyayisil($uname);
if($silmi){
echo "<script>
alert('Şifreniz Başarıyla Değiştirildi.');
window.location.href='index.php';
</script>";}
else{
echo "<script>
alert('Hata Oluştu.');
window.location.href='index.php';
</script>";}
}else{
echo "<script>
alert('Kullanıcı Adı yada Şifrede Hata.');
window.location.href='index.php';
</script>";
}
}
function csvyaz($uname,$password,$newPassword)
{
$data = array("samaccountname,oldpassword,newpassword",
$uname.",".$password.",".$newPassword);
$fp = fopen('C:\Reset\PasswordReset.csv', 'w');
foreach ( $data as $line ) {
$val = explode(",", $line);
fputcsv($fp, $val);
}
fclose($fp);
}
function telefoncsvyaz($uname,$telno1,$telno2)
{
$tel="+90".$telno1.$telno2;
$data = array("samaccountname,mobilephone",
$uname.",".$tel);
$fp = fopen('C:\Reset\TelReset.csv', 'w');
foreach ( $data as $line ) {
$val = explode(",", $line);
fputcsv($fp, $val);
}
fclose($fp);
}
function telefonlarcsv($uname,$telno1,$telno2)
{
$varmi=file_exists("C:/Reset/Telefonlar.csv");
if ($varmi){
$data = array($uname.",".$telno1.",".$telno2);
}
else{
$data = array("samaccountname,telno1,telno2",$uname.",".$telno1.",".$telno2);
}
$fp = fopen('C:\Reset\Telefonlar.csv', 'a');
foreach ( $data as $line ) {
$val = explode(",", $line);
fputcsv($fp, $val);
}
fclose($fp);
}
function powershellverify($uname,$password,$newPassword)
{
$myfile = fopen("c:/Reset/".$uname.".txt", "w");
// $output2 = shell_exec('powershell.exe -c "C:\wwwroot\logoncss\8-login-form\PasswordVerify.ps1"');
// return $output2;
return $myfile;
}
function powershellreset()
{
$output2 = shell_exec('powershell.exe -c "C:\wwwroot\logoncss\8-login-form\PasswordReset.ps1"');
// return $output2;
}
function powershelltelephone()
{
$psPath = 'c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned c:\wwwroot\logoncss\8-login-form\Telephone.ps1';
$output2 =shell_exec($psPath);
// $output2 =exec('powershell.exe "C:\wwwroot\logoncss\8-login-form\Telephone.ps1"');
return $output2;
}
function dosyayikontrolet($uname)
{
$varmi=file_exists("C:/Reset/".$uname.".txt");
return $varmi;
}
function dosyayisil($uname)
{
$silmi=unlink("C:/Reset/".$uname.".txt");
return $silmi;
}
?>
my_script.js
function validatePassword() {
var output = true;
//var formdata = jQuery( "#form1" ).serialize();
username = document.form1.uname;
telno1 = document.form1.telno1;
// telno2 = document.form1.telno2;
password = document.form1.password;
newPassword = document.form1.newPassword;
new2Password = document.form1.new2Password;
tip = document.form1.tip;
if(!username.value) {
username.focus();
document.getElementById("unametxt").innerHTML = "Boş bırakılamaz.";
output = false;
}
else if(!telno1.value) {
// telno1.focus();
document.getElementById("telno1txt").innerHTML = "Boş bırakılamaz.";
output = false;
}
else if((telno1.value)){
var paswd= /^(?=.*\d)(?=.*[0-9])(?!.*\s)/;
if((!telno1.value.match(paswd)))
{
// telno1.focus();
document.getElementById("telno1txt").innerHTML = "Kod kısmında hata";
output=false;
}
}
else if(!password.value) {
// password.focus();
document.getElementById("passwordtxt").innerHTML = "Boş bırakılamaz.";
output = false;
}
else if(!newPassword.value) {
// newPassword.focus();
document.getElementById("newPasswordtxt").innerHTML = "Boş bırakılamaz.";
output = false;
}
else if(!new2Password.value) {
new2Password.focus();
document.getElementById("new2Passwordtxt").innerHTML = "Boş bırakılamaz.";
output = false;
}
else if(newPassword.value!=new2Password.value) {
newPassword.focus();
//newPassword.value="";
//confirmPassword.value="";
document.getElementById("new2Passwordtxt").innerHTML = "Yeni şifreler uyuşmadı.";
output = false;
}
/* else if(!telno2.value) {
telno2.focus();
document.getElementById("telno2txt").innerHTML = "Boş bırakılamaz.";
output = false;
}
else if((telno2.value)){
var paswd2= /^(?=.*\d)(?=.*[0-9])(?!.*\s)/;
if((!telno2.value.match(paswd2)))
{
telno2.focus();
document.getElementById("telno2txt").innerHTML = "Telefonda hata";
output=false;
}
}*/
return output;
}
function myFunction() {
document.getElementById("ogrenci").value="@kastamonu.edu.tr";
}
function myFunction1() {
document.getElementById("ogrenci").value="@ogr.kastamonu.edu.tr";
}
/*function simdikisifre() {
password = document.form1.password;
alert(password.value );
//var paswd= /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?!.*\s).{5,10}$/;
var paswd= /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{7,15}$/;
if(!password.value.match(paswd))
{
alert('Şifreniz 7 ile 15 karakter arasında bir uzunlukta olmalı.En az bir sayısal ve özel karakter içermelidir.')
output=false;
}
}*/
最佳答案
除去其他情况
并确定是否需要检查所有情况。
if(!username.value) {
username.focus();
document.getElementById("unametxt").innerHTML = "Boş bırakılamaz.";
output = false;
}
if(!telno1.value) {
// telno1.focus();
document.getElementById("telno1txt").innerHTML = "Boş bırakılamaz.";
output = false;
}
if((telno1.value)){
var paswd= /^(?=.*\d)(?=.*[0-9])(?!.*\s)/;
if((!telno1.value.match(paswd)))
{
// telno1.focus();
document.getElementById("telno1txt").innerHTML = "Kod kısmında hata";
output=false;
}
}
if(!password.value) {
// password.focus();
document.getElementById("passwordtxt").innerHTML = "Boş bırakılamaz.";
output = false;
}
if(!newPassword.value) {
// newPassword.focus();
document.getElementById("newPasswordtxt").innerHTML = "Boş bırakılamaz.";
output = false;
}
if(!new2Password.value) {
new2Password.focus();
document.getElementById("new2Passwordtxt").innerHTML = "Boş bırakılamaz.";
output = false;
}
if(newPassword.value!=new2Password.value) {
newPassword.focus();
//newPassword.value="";
//confirmPassword.value="";
document.getElementById("new2Passwordtxt").innerHTML = "Yeni şifreler uyuşmadı.";
output = false;
}
return output;
关于javascript - 使用Ajax进行PHP表单验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26013604/