我知道有幾個類似的問題,但沒有給我一個答案。 我停止驗證用戶輸入,並直接進入Activation.php 它在一開始工作,但後來我添加了我的驗證碼,如果語句,它停了下來。 任何想法爲什麼它不再工作?HTML Javascript onsubmit忽略我的javascript功能
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html version="-//W3C//DTD XHTML 1.1//EN"
xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/1999/xhtml
http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd"
>
<head>
<link rel="stylesheet" type=text/css" href="stylesheet.css">
<script>
function validateForm() {
var username = document.Register.uname;
var password = document.Register.pword;
var email = document.Register.email;
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var passw = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,41}$/;
var str1 = removeSpaces(document.getElementById('captchaimg').value);
var str2 = removeSpaces(document.getElementById('txtInput').value);
if (username.value == "")
{
alert("Username is required.");
username.focus();
return false;
}
if (password.value == "")
{
alert("Please enter a password.");
password.focus();
return false;
}
if (!passw.test(password.value))
{
alert("Password must consist of 8 to 41 characters, contain at least on digit and at least 1 upper case character");
password.focus();
return false;
}
if (email.value == "")
{
alert("Please enter your email address.");
email.focus();
return false;
}
if (!filter.test(email.value))
{
alert("Please enter a valid email address.");
email.focus();
return false;
}
if (str1 != str2)
{
alert("Captcha is incorrect");
return false;
}
else{
return true;
}
}
</script>
</head>
<body>
<form action="Activation.php" name="Register" accept-charset="UTF-8" method = 'POST'onsubmit = "return validateForm()">
E-mail: <input type="text" name="email" id = "emailid" value="<?PHP if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']); ?>" size= "30" maxlength="30" ><br><br>
Username: <input type="text" name="uname" id = "unameid" value="<?PHP if(isset($_POST['uname'])) echo htmlspecialchars($_POST['uname']); ?>" > <label id="UserLabelId"><>
<br><br>
Password: <input type="password" maxlength="8" name="pword" id = "pwordid" value="<?PHP if(isset($_POST['pword'])) echo htmlspecialchars($_POST['pword']); ?>" size= "25" maxlength="25" ><br><br>
Enter Captcha code below: </br>
<img src ="captcha.php?rand=<?php echo rand();?>" id='captchaimg'><br>
<input id="6_letters_code" id="txtInput" name="6_letters_code" type="text" value="<?PHP if(isset($_POST['6_letters_code'])) echo htmlspecialchars($_POST['6_letters_code']); ?>" placeholder="Type captcha here" ><br>
<input type="hidden" id="txtCaptcha" /></label>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </br>
<input type="submit" value="Register" name="submit" class="quoteButton">
</form>
<script language='JavaScript' type='text/javascript'>
function refreshCaptcha(){
var img = document.images['captchaimg'];
img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>
</body>
</html>`enter code here`
如果添加之間''POST''和'onsubmit'缺少的空間會發生什麼:'<形式行動= 「Activation.php」 NAME = 「註冊」 接收字符= 「UTF-8」 的方法='POST'onsubmit =「return validateForm()」>'? –
@PaulRoub我認爲這不重要。該引號分隔了該屬性,因此不需要空間。 – Barmar
'captchaimg'是一個圖像,而不是輸入。它沒有'.value'。 – Barmar