0

私は現在、単純なPHPスクリプトを使用してWebフォームからSQLデータベースに一連の情報を投稿するフォームの非常に基本的な検証に取り組んでいます。PHPスクリプトは正常に機能しており、値はデータベースに投稿されていますが、私が作成した検証は無視されているようです。私はしばらくの間それを修正しようとしました、そして明白な間違いを見ることができません、しかしそれが些細なことであるならば謝罪します。

HTML:

<form name = "registerForm" method = "POST" onsubmit = "return ValidateRegistration()" action = "createUser.php">
    <p class = "register">Desired Username*</p>
    <p class = "registerDesc">Between 1 and 20 characters long</p>                  
    <input type = "text" name = "username" class = "register">
    <p class = "register">Desired Password*</p>
    <p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p>
    <input type = "password" name = "password" class = "register">
    <p class = "register">Confirm Password*</p>
    <input type = "password" name = "confirmPassword" class = "register">
    <p class = "register">Email Adress*</p>
    <input type = "text" name = "email" class = "registerEmail">
    <p class = "register">Bio</p>
    <p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p>
    <textarea name = "bio" class = "registerBio"></textarea>
    <p><input type = "submit" class = "registerButton" value = "Register"></p>
</form>

JavaScript:

function ValidateRegistration()
{
    //username block
    var username=document.forms["registerForm"]["username"].value;
    if (username==null || username==" ")
    {
        alert("You must provide a username");
        return false;
    }       
    if (username.length>20 || username.length<1)
    {
        alert("Sorry, your username must be between 1 and 20 characters long.");
        return false;
    }
    //etc....
}
4

5 に答える 5

2

まさにあなたのコードは私のために働いています:

次のtest.htmlファイルを作成し、chromeで開きました。

<html>
<head>
<script>
function ValidateRegistration()
{
    //username block
    var username=document.forms["registerForm"]["username"].value;
    if (username==null || username==" ")
    {
        alert("You must provide a username");
        return false;
    }       
    if (username.length>20 || username.length<1)
    {
        alert("Sorry, your username must be between 1 and 20 characters long.");
        return false;
    }
    //etc....
}
</script>
</head>
<body>
<form name = "registerForm" method = "POST" onsubmit = "return ValidateRegistration()" action = "createUser.php">
    <p class = "register">Desired Username*</p>
    <p class = "registerDesc">Between 1 and 20 characters long</p>                  
    <input type = "text" name = "username" class = "register">
    <p class = "register">Desired Password*</p>
    <p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p>
    <input type = "password" name = "password" class = "register">
    <p class = "register">Confirm Password*</p>
    <input type = "password" name = "confirmPassword" class = "register">
    <p class = "register">Email Adress*</p>
    <input type = "text" name = "email" class = "registerEmail">
    <p class = "register">Bio</p>
    <p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p>
    <textarea name = "bio" class = "registerBio"></textarea>
    <p><input type = "submit" class = "registerButton" value = "Register"></p>
</form>
</body>
</html>

空のフォームアラートの送信:

申し訳ありませんが、ユーザー名は1〜20文字の長さである必要があります。

于 2012-12-28T23:19:27.227 に答える
1

http://fiddle.jshell.net/hmariod/LRZkR/

<form name = "registerForm" method = "POST"  action = "createUser.php">
    <p class = "register">Desired Username*</p>
    <p class = "registerDesc">Between 1 and 20 characters long</p>                  
    <input type = "text" name = "username" class = "register">
    <p class = "register">Desired Password*</p>
    <p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p>
    <input type = "password" name = "password" class = "register">
    <p class = "register">Confirm Password*</p>
    <input type = "password" name = "confirmPassword" class = "register">
    <p class = "register">Email Adress*</p>
    <input type = "text" name = "email" class = "registerEmail">
    <p class = "register">Bio</p>
    <p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p>
    <textarea name = "bio" class = "registerBio"></textarea>
    <p><input type = "button" onclick="ValidateRegistration();" class = "registerButton" value = "Register"></p>
</form>​

function ValidateRegistration(){
    try{
        var username=document.forms["registerForm"]["username"].value;
        if (username.length < 1)
        {
            alert("You must provide a username");
            return;
        }       
        if (username.length>20 || username.length<1)
        {
            alert("Sorry, your username must be between 1 and 20 characters long.");
            return;
        }
        document.forms["registerForm"].submit();
    }catch(er){
         alert(er);
    }
}​
于 2012-12-28T23:25:37.840 に答える
0

私はonsubmitでリターンを取り除くでしょう。

于 2012-12-28T22:55:48.427 に答える
0

コードをtry/catchブロック内に配置して、何が起こっているかを確認します。

try{
  //your code
}catch(er){
  alert(er);
}
于 2012-12-28T23:06:21.900 に答える
0

問題はあなたの比較行にあると思います

if (username==null || username==" ") 

どちらであるべきか

if (username==null || username=="")

問題が解決することを願っています。私にお知らせください ;-)

于 2013-03-01T15:59:03.980 に答える