0

onsubmitJavaScriptで検証を呼び出す必要がある課題がありました。この部分は見事に機能しました。

2 番目の部分は、サーバー側の PHP 検証を組み込むことでした。コーディングはおそらく同じページにあるはずです。したがって、以下にあるのは、私が見つけたいくつかの調査と例であり、これが私が思いついたものです:

<?php
echo "<form action='' method='post' onsubmit='return regValidation()' id='RegistrationForm'>";
echo "  <div>";
echo "  <span class='reglbl'>";
echo "      First Name: <input type='text' name='FirstName' id='FirstNameID'/>";
echo "      Last Name: <input type='text' name='LastName' id='LastNameID'/>";
echo "  </span>";
echo "  </div>";
echo "  <div>";
echo "  <span class='reglbl'>";
echo "      Email Address: <input type='text' name='email' id='EmailID'/>";
echo "      Phone Number: <input type='text' name='PhoneNum' id='PhoneNumID'/>";
echo "  </span>";
echo "  </div>";
echo "  <div class='reglbl'>Pssword: <input type='password' name='PWD1' id='PWD1'/></div>";
echo "  <div class='reglbl'>Confirm Password: <input type='password' name='PWD2' id='PWD2'/></div>";
echo "  <div><input type='submit' value=' Register ' id='sub' /></div>";
echo "</form>";
if(!isset($_REQUEST['FirstName']) ||
    !isset($_REQUEST['LastName']) ||
    !isset($_REQUEST['email']) ||
    !isset($_REQUEST['PhoneNum']) ||
    !isset($_REQUEST['PWD1']) ||
    !isset($_REQUEST['PWD2']) ||
    $_REQUEST['FirstName'] =="" ||
    $_REQUEST['LastName'] == "" ||
    $_REQUEST['email'] == "" ||
    $_REQUEST['PhoneNum'] == "" ||
    $_REQUEST['PWD1'] == "" ||
    $_REQUEST['PWD2']=="") 
{
    echo "<script>alert('Please enter all fields!')</script>";
} elseif($_REQUEST['PWD1']!=$_REQUEST['PWD2']) {
    echo "<script>alert('Passwords entered do not match!')</script>";
} elseif(strlen($_REQUEST['PWD1'])<8||strlen($_REQUEST['PWD2'])<8) {
    echo "<script>alert('Passwords must be 8 or more characters!')</script>";
} else {
    // CALL A FUNCTION IN PHP HERE
}
?>

したがって、Java と PHP の両方の検証は同じです。唯一の違いは、PHP がパスワードの 8 文字の長さをもう少しチェックすることですが、このコードが実行されると JavaScript のみが検証されます。

また、最後のelse用のスクリプトを含む外部PHPを呼び出す方法を知りたいです。

/REGISTAR VALIDATION function regValidation(){ の場合の JavaScript コードは次のとおりです。

var firstName = document.getElementById('FirstNameID');
var lastName = document.getElementById('LastNameID');
var email = document.getElementById('EmailID');
var pwd1 = document.getElementById('PWD1');
var pwd2 = document.getElementById('PWD2');


if(firstName.value == '' || lastName.value == '' || email.value == '' || pwd1.value == '' || pwd2.value == '')
{
    alert("Please enter all fields!");
}
else if (pwd1.value !== pwd2.value)
{
    alert("Passwords entered do not match!");   
}
else
{

}
return false;

}

4

1 に答える 1

0

これを試してみてください

<?php
echo "<form action='' method='post' onsubmit='return regValidation()' id='RegistrationForm'>";
echo "  <div>";
echo "  <span class='reglbl'>";
echo "      First Name: <input type='text' name='FirstName' id='FirstNameID'/>";
echo "      Last Name: <input type='text' name='LastName' id='LastNameID'/>";
echo "  </span>";
echo "  </div>";
echo "  <div>";
echo "  <span class='reglbl'>";
echo "      Email Address: <input type='text' name='email' id='EmailID'/>";
echo "      Phone Number: <input type='text' name='PhoneNum' id='PhoneNumID'/>";
echo "  </span>";
echo "  </div>";
echo "  <div class='reglbl'>Pssword: <input type='password' name='PWD1' id='PWD1'/></div>";
echo "  <div class='reglbl'>Confirm Password: <input type='password' name='PWD2' id='PWD2'/></div>";
echo "  <div><input type='submit' name='formSubmit'value='Register' id='sub' /></div>";
echo "</form>";
if(isset($_REQUEST['formSubmit']) && $_REQUEST['formSubmit'] == 'Register')
{
if(!isset($_REQUEST['FirstName']) ||
    !isset($_REQUEST['LastName']) ||
    !isset($_REQUEST['email']) ||
    !isset($_REQUEST['PhoneNum']) ||
    !isset($_REQUEST['PWD1']) ||
    !isset($_REQUEST['PWD2']) ||
    $_REQUEST['FirstName'] =="" ||
    $_REQUEST['LastName'] == "" ||
    $_REQUEST['email'] == "" ||
    $_REQUEST['PhoneNum'] == "" ||
    $_REQUEST['PWD1'] == "" ||
    $_REQUEST['PWD2']=="") 
{
    echo "<script>alert('Please enter all fields!')</script>";
} elseif($_REQUEST['PWD1']!=$_REQUEST['PWD2']) {
    echo "<script>alert('Passwords entered do not match!')</script>";
} elseif(strlen($_REQUEST['PWD1'])<8||strlen($_REQUEST['PWD2'])<8) {
    echo "<script>alert('Passwords must be 8 or more characters!')</script>";
} else {
    // CALL A FUNCTION IN PHP HERE
}
}
?>

javascript 関数を使用すると、パスワードの長さの検証をさらに追加できます

<script>
function regValidation(){

var firstName = document.getElementById('FirstNameID');
var lastName = document.getElementById('LastNameID');
var email = document.getElementById('EmailID');
var pwd1 = document.getElementById('PWD1');
var pwd2 = document.getElementById('PWD2');


if(firstName.value == '' || lastName.value == '' || email.value == '' || pwd1.value == '' || pwd2.value == '')
{
    alert("Please enter all fields!");
}
else if (pwd1.value !== pwd2.value)
{
    alert("Passwords entered do not match!");   
}
else
{
    return true;
}
return false;
}
</script>
于 2012-08-03T13:13:20.060 に答える