使用我的css大约一个小时后,我的html表单的javascript验证突然停止工作。真的无法弄清楚出了什么问题,因为我根本没碰过我的html或js文件..
function validateForm()
{
var validName = document.forms["myForm"]["namn"].value;
var validEmail = document.forms["myForm"]["email"].value;
var validMsg = document.forms["myForm"]["medd"].value;
if (validName == null || validName == "")
{
alert("Please enter a name.");
return false;
}
if (validEmail == null || validEmail == "" validEmail.trim() == null)
{
alert("Please enter a valid Email.");
return false;
}
if(validName.trim() == null || validName.trim() == '')
{
alert("inga mellanslag!");
return false;
}
if (validEmail.indexOf('@') === -1)
{
alert("enter valid email");
return false;
}
if(validEmail.indexOf('.') === -1)
{
alert("enter valid email2");
return false;
}
if(validMsg.trim() == null || validMsg.trim() == '')
{
alert("enter valid message");
return false;
}
}
<script src="main.js"> </script>
<div class="kommentar">
<form name="myForm" action="db.php" method="post">
<p>First name:</p>
<input class="rutor" type="text" name="namn"><br>
<p>Email:</p>
<input class="rutor" type="text" name="email"><br>
<p>Message:</p>
<textarea class="meddelande" type="text" name="medd" ></textarea>
<br>
<button type="submit" class="knapp" value="Kommentera" onclick="return validateForm()">Kommentera</button>
</form>
答案 0 :(得分:0)
请更改
行if (validEmail == null || validEmail == "" validEmail.trim() == null)
到
if (validEmail == null || validEmail == "" || validEmail.trim() == null)
并告诉我这是否有帮助