2013-08-12 50 views
-7

MyFunction.js如何驗證形式使用jQuery

function validateForm() { 
    var frm = document.login; 
    if(frm.username.value=="abc" && frm.password.value=="abc123"){ 
     alert(); 
    }else { 
     alert(); 
    } 
} 

MyForm.php

<form id="login" name="login" action="" method="post" onsubmit="return validateForm()" > 
    <h3>My Login Form</h3> 
    <p>Username : <input type="text" name="username" id="username" /></p> 
    <p>Password : <input type="password" name="password" id="password" /></p> 
    <p><input type="submit" name="submit" id="submit" value="Login" /></p> 
</form> 

我想有效的這兩個值等於ABC & & ABC123 如何我提交?

更新後的問題 - 2017年1月15日

我在看我的歷史,發現一些問題,我問愚蠢的方式。是啊!每個專家都是初學者。沒有!我還不是專家。

如何驗證表單提交使用jQuery?

MyFunction.js

function validateForm() { 
    var frm = document.login; 
    if(frm.username.value=="abc" && frm.password.value=="abc123"){ 
     alert(); 
    }else { 
     alert(); 
    } 
} 

MyForm.php

<form id="login" name="login" action="" method="post" onsubmit="return validateForm()" > 
    <h3>My Login Form</h3> 
    <p>Username : <input type="text" name="username" id="username" /></p> 
    <p>Password : <input type="password" name="password" id="password" /></p> 
    <p><input type="submit" name="submit" id="submit" value="Login" /></p> 
</form> 
+0

你的問題是不清楚的。你想如果$ user和$ pass被給出,那麼它會移動到下一頁? – ABorty

+1

你已經嘗試了什麼?顯示你做了什麼,並解釋爲什麼它不工作。 –

+1

「我如何寫這些代碼?」通過嘗試。我沒有看到一個嘗試。 – woofmeow

回答

0

這就是我正在尋找的代碼。我認爲我的問題並沒有像我的朋友所說的那樣被清除。無論如何!我終於寫了我自己的代碼。這是一個簡單的表單,如果你想要通過表單,你必須輸入USERNAME作爲abc,PASSWORD作爲abc123。

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame Remove this if you use the .htaccess --> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
     <title>test2</title> 
     <meta name="description" content=""> 
     <meta name="author" content="ITS The 1 - Yesh"> 
     <meta name="viewport" content="width=device-width; initial-scale=1.0"> 
     <!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references --> 
     <link rel="shortcut icon" href="/favicon.ico"> 
     <link rel="apple-touch-icon" href="/apple-touch-icon.png"> 
     <script> 
      function validateForm() { 
       var frm = document.login; 
       if(frm.username.value=="abc" && frm.password.value=="abc123"){ 
        alert ("You Are Doing Well"); 
       }else { 
        alert ("Please Fill Your Username And Password!"); 
        username.focus(); 
        return false; 
       } 
      } 
     </script> 
    </head> 
    <body> 
     <div> 
      <form id="login" name="login" action="" method="post" onsubmit="return validateForm()" > 
       <h3>My Login Form</h3> 
       <p>Username : <input type="text" name="username" id="username" /></p> 
       <p>Password : <input type="password" name="password" id="password" /></p> 
       <p><input type="submit" name="submit" id="submit" value="Login" /></p> 
      </form> 
     </div> 
    </body> 
</html> 

謝謝你們!