Reputation: 31
I am trying to do Form validation to validate the information request form by checking that the following fields in the form have been filled out:
▪ Name field
▪ Email field
▪ Comments
My function validateName() does not work at all it causes the other functions not to work either (adding default values to fields and removal of defaults when text area is clicked). When I remove the validateName() the functions I previously created work fine. It should be noted that I have addded the onsubmit="return validateName()" to the form element. I will also need to create a validateEmail() and validateComments() respectively.
I would also like the function for validateName(), validateEmail(), and validateComments() validation to check the following:
▪ Check if the form field is empty when submitted ▪ Check if the default text is in the form field when submitted
I have commented out the 2 validateName() functions I attempted to use so my other functions will work.
function formtext(){
document.contact.Name.value="Enter your name.";
document.contact.Email.value="Enter your email address.";
document.contact.questions.value="Enter your comments.";
}
function delete_email(){
document.contact.Email.value = "";
}
function delete_name(){
document.contact.Name.value = "";
}
function delete_comments(){
document.contact.questions.value = "";
}
/* function validateName()
{
var x=document.contact.Name.value;
if (x==null || x=="")
{
alert("Name must be filled out");
return false;
}
} */
/* function validateName()
{
var x=document.contact.["Name"].value;
if (x==null || x=="")
{
alert("Name must be filled out");
return false;
}
} */
</script>
Upvotes: 3
Views: 126
Reputation: 2904
var defaultName == 'Please Enter Name', defaultEmail == 'Please Enter Email', defaultComment == 'Please Enter Comment';
function validateForm()
{
if(document.getElementById('namefield').value == '' || (document.getElementById('namefield').value == defaultName)
{
alert('Please enter name');
}
if(document.getElementById('emailfield').value == '' || (document.getElementById('emailfield').value == defaultEmail)
{
alert('Please enter email');
}
if(document.getElementById('commentsfield').value == '' || (document.getElementById('commentsfield').value == defaultComment)
{
alert('Please enter comments');
}
}
Upvotes: 2