我有这个 .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;
}
}
}