PHP 文档中的 JavaScript 验证问题



我有这个.php文档,您可以在网上商店注册。我想为它添加javascript验证,但该网站完全由.php组成,因此没有html头和正文标签。通常你可以把javascript源代码放在head标签中,但在.php你不能。(或者至少我不知道怎么做)在测试代码时,javascript 文档似乎很好,但是当我把它放在.php文档中时,它会给出一些关于 (") 和 (') 的错误。当我尝试链接到单独的javascript文档时,代码似乎根本不起作用。

(我已经添加了整个PHP页面)Php文档开始:

<?php
include 'header.php';
include 'menu.php';
    echo '

function checkEmail(myForm) 
{
    if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(registration.emailAddress.value))
    {
    return (true)
    }
        alert("Invalid E-mail Address! Please re-enter.")
        return (false)
}
function validatePwd() 
{
    var invalid = " "; // Spatie mag niet
    var minLength = 6; // Minimaal 6 tekens
    var pw1 = document.registration.password.value;
    var pw2 = document.registration.passwordConfirm.value;
// Beide velden gelijke waarde
    if (pw1 == "" || pw2 == "") 
    {
        alert("Please enter your password twice.");
        return false;
    }
// Minimale lengte variabele
    if (document.registration.password.value.length < minLength) 
    {
        alert("Your password must be at least " + minLength + " characters long. Try again.");
        return false;
    }
// spatiegebruik variabele
    if (document.registration.password.value.indexOf(invalid) > -1); 
    {
        alert("Spaces are not allowed.");
        return false;
    }
    else 
    {
    if (pw1 != pw2) 
        {
        alert ("You did not enter the same new password twice. Please re-enter your password.");
        return false;
        }
    else 
    {
        return true;
    }
    }
}

        <div id="main">
    <form name="registration" action="registerconfirm.php" method="post" id="form" onSubmit="return validatePwd()">

        <h2>Personal Information</h2>

        <fieldset class="login_register">

            <label for="firstName" class="form">First name:</label> 
                <div class="registrationForm" id= "firstNameCheck"></div>
                    <input type="text" name="firstname" id="register_first_name"/>

            <label for="lastName" class="form">Last name:</label>
                <div class="registrationForm" id="lastNameCheck"></div>
                    <input type="text" name="lastname" id="register_last_name"/>

            <label for="email" class="form">E-mail:</label>
                <div class="registrationForm" id="emailCheck"></div>
                    <input type="text" name="emailAddress" id="register_email"/>

        </fieldset>

        <h2>Login Information</h2>
        <fieldset class="login_register">

            <label for="password" class="form">Password:</label>
                <div class="registrationForm" id="lastNameCheck"></div>                 
                    <input type="password" name="password" id="password"/>

            <label for="passwordCheck" class="form">Confirm password:</label>
                <div class="registrationFormAlert" id="divPassCheck"></div>
                    <input type="password" name="passwordConfirm" id="passwordConfirm"/>

            <input type="submit" class="button" value="SUBMIT"/>
        </fieldset>
    </form>
</div>
';
include 'footer.php';

?>

和Javascript代码:

<SCRIPT LANGUAGE="JavaScript">
function checkEmail(myForm) 
{
    if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(registration.emailAddress.value))
    {
    return (true)
    }
        alert("Invalid E-mail Address! Please re-enter.")
        return (false)
}
function validatePwd() 
{
    var invalid = " "; // Invalid character is a space
    var minLength = 6; // Minimum length
    var pw1 = document.myForm.password.value;
    var pw2 = document.myForm.passwordConfirm.value;
// check for a value in both fields.
    if (pw1 == '' || pw2 == '') 
    {
        alert('Please enter your password twice.');
        return false;
    }
// check for minimum length
    if (document.myForm.password.value.length < minLength) 
    {
        alert('Your password must be at least ' + minLength + ' characters long. Try again.');
        return false;
    }
// check for spaces
    if (document.myForm.password.value.indexOf(invalid) > -1) 
    {
        alert("Sorry, spaces are not allowed.");
        return false;
    }
    else 
    {
    if (pw1 != pw2) 
        {
        alert ("You did not enter the same new password twice. Please re-enter your password.");
        return false;
        }
    else 
    {
        return true;
    }
    }
}

有几件事:您可以在任何您喜欢的地方添加<script>标签。属性language 虽然并不存在,但这并不那么重要。尽管如此,它应该是<script type="text/javascript">.
如果您的网站是(一堆)PHP脚本的输出,为什么不简单地echo脚本呢?

echo '<script type="text/javascript" src="path/to/script.js"></script>';

或者,在头文件中:

    echo '<title>Title</title>';
?>
<script type='text/javascript'>
    var script ='just add it here';
</script>
<?php
    echo '</head>';//continue php

也没有什么能阻止你这样做。

JavaScript在

客户端浏览器(又名客户端)中执行,PHP是一种服务器端语言。

除非你打算为 PHP 编写一个 JS 解释器,否则你不能从 PHP 运行 JS。
更好的选择是用PHP重写JS。


除非您在包含文件时遇到问题,否则您只需编辑以下内容:
<script type="text/javascript" src="myJSFile.js"></script>标签到您的header.php中。

所以:

echo '<script type="text/javascript" src="myJSFile.js"></script>';

尝试使用 PHP HEREDOC 语法来输出函数。http://www.php.net/manual/it/language.types.string.php#language.types.string.syntax.heredoc

相关内容

  • 没有找到相关文章

最新更新