-1

Where am I going wrong? I can get the javascript to validate that an email address has been entered, but not that the name and comments box are not empty. Can you please help?

<!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 code

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;  
} 
}

I just cant work out exactly where I am going wrong here. What am I missing?

Sparky
  • 98,165
  • 25
  • 199
  • 285
pebs
  • 11
  • 4
  • `{ }` are not closing properly – prasanth Apr 17 '17 at 10:00
  • Do not use the jQuery Validate tag when you're not using the jQuery Validate plugin... you're not even using jQuery here. Edited. Also, for troubleshooting, it would be beneficial to you and everyone here to properly format and indent your code. [Allman style should not be used for JavaScript](http://stackoverflow.com/a/11247362/594235). – Sparky Apr 17 '17 at 15:21

1 Answers1

0

You have a syntax error at this line: alert("Name should not be empty);

properly close it alert("Name should not be empty");

user7761868
  • 94
  • 1
  • 6
  • Thanks, didn't even notice that. I have fixed it now, but still having same issues. It doesn't do anything if the name text box is empty, but if the email is in the wrong format it alerts. – pebs Apr 17 '17 at 10:46
  • @pebs, you made this same mistake in TWO different places. – Sparky Apr 17 '17 at 15:24