使用Javascript验证文本不为空

时间:2017-04-17 09:53:02

标签: javascript forms validation

我哪里错了? 我可以使用javascript来验证是否已输入电子邮件地址,但不是名称和注释框不为空。 你能帮忙吗?

<!DOCTYPE html>  
<html>
<head>  
<meta charset="utf-8">  
<title>Javascript Form Validation</title>
<script src="formvalidationjs.js"></script>
</head>
<body>

<h1>Contact Us</h1>

<form name='contactform' onSubmit="return validateform();">
<label for='name'>Name: </label></li> <li><input type="text" name="name" /></br>
<label for='email'>Email: </label></li> <li><input type="email" name="email" /></br>
<label for='phone'>Phone: </label></li> <li><input type="text" name="phone" /></br>

</br><label for='comment'>Comments: </label></li> <li><input type="text" name="comment" />
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
</body>
</html>

JavaScript代码

function validateform()
{
var uname = document.contactform.name;
var uemail = document.contactform.email;
var uphone = document.contactform.phone;
var ucomment = document.contactform.comment;

{
if(allLetter(uname))
{
if(ValidateEmail(uemail))
{
if(alphanumeric(uphone))
{
return false;

function validate(uname)
{
if (uname.value.length == 0)
{  
alert("Name should not be empty);  
return false;  
}  
return true;  
} 

function validate(uphone)
{
var uphone_len = uphone.value.length;
if (uphone_len == 0 || uphone_len=="" )
{  
alert("Phone should not be empty);  
uphone.focus();  
return false;  
}  
return true;  
} 

function Validate(uemail)  
{  
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;  
if(uemail.value.match(mailformat))  
{  
return true;  
}  
else  
{  
alert("You have entered an invalid email address!");  
uemail.focus();  
return false;  
}     

function validate(ucomment)
{
var ucomment_len = ucomment.value.length;
if (ucomment_len == 0 || ucomment_len=="" )
{  
alert("Comment should not be empty);  
ucomment.focus();  
return false;  
}  
return true;  
} 
}

我无法确切地知道我在哪里错了。 我错过了什么?

1 个答案:

答案 0 :(得分:0)

此行有一个语法错误: [DllImport("User32.dll")] private static extern IntPtr WindowFromDC(IntPtr hDC); // override: depending on MyClass it can be protected virtual void... protected override void OnPaint(PaintEventArgs e) { ... IntPtr hDC = e.Graphics.GetHdc(); try { IntPtr hWnd = WindowFromDC(hDC); ... } finally { e.Graphics.ReleaseHdc(hDC); } }

正确关闭alert("Name should not be empty);