gpt4 book ai didi

php - PHP 文档中的 javascript 验证问题

转载 作者:行者123 更新时间:2023-11-30 18:10:48 25 4
gpt4 key购买 nike

我有这个 .php 文档,您可以在其中注册网上商店。我想向它添加 javascript 验证,但该站点完全由 .php 组成,因此没有 html head 和 body 标签。通常你可以将 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

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

关于php - PHP 文档中的 javascript 验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14603180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com