0

我有一个任务需要onsubmit在 JavaScript 中调用验证。这部分工作得很好。

第二部分是合并服务器端的 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。

这是 javascript 代码 incase /REGISTAR VALIDATION 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 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 回答