使用ajax进行PHP表单验证

时间:2014-09-24 09:41:01

标签: javascript php ajax forms validation

我想验证我的表格。所以我创建my_script.js并使用它的形式的onsubmit事件。

<form id="form1" name ="form1" method="POST" action="index.php" onSubmit="return validatePassword()">

但是脚本不能完全正常工作。 uname,telno1是验证,但其他如果不工作。 例如,如果username或telno1为null并单击submit1按钮,则在unametxt或telno1txt上写入javascript工作和错误。但如果密码或其他为null并单击submit1按钮,则表单已发布。 我不明白为什么两个如果声明工作,其他人不。

的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">
      &copy; 2014&ndash;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;

    }
  }*/

2 个答案:

答案 0 :(得分:0)

如果是的话,删除其他 并且如果情况如此,它将检查所有。

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;

答案 1 :(得分:0)

那是因为只有在没有电话号码的情况下才会访问你的if(!密码)(因为你正在使用其他电话号码)。

它的作用是它将检查电话号码,如果有,它将不会执行任何其他if语句。

只需使用case语句,或者至少只使用不带else的语句。

相关问题