Nian
Nian

Reputation: 171

HTML form validation issue

<script type="text/javascript">
function check(n,mail,msg)
{
if(name.toLowerCase()== "")
{
alert("Please Enter your name.");
document.getElementById("Name").select();
    document.getElementById("Name").focus();
    return false;
}
else if(lname.toLowerCase()== "")
{
    alert("Please Enter your email.")
    document.getElementById("email").select();
    document.getElementById("email").focus();   
    return false;
}
else if(ph == "")
{ 
    alert("Please Enter your Contact Number.")
    document.getElementById("message").focus();
    return false;
}

return true;
}

=========================================================

<form method="post" name="contact_form" action="contact-form-handler.php">

<p> Your Name:
<input type="text" name="name"></p>

<p>Email Address:
<input type="text" name="email"></p>

<p>Message:
<textarea name="message"></textarea></p>

=========================================================

<input type="button" value="submit" onclick="return check(document.getElementById('name').value,document.getElementById('email').value,document.getElementById('message').value)" /> 

=========================================================

i am having issues with the HTML validation as when i clicked the button, there is no validation performed.

Upvotes: 1

Views: 94

Answers (3)

Jebin
Jebin

Reputation: 742

<form method="post" name="contact_form" action="contact-form-handler.php">

<p> Your Name:
<input type="text" id="name" name="name"></p>

<p>Email Address:
<input type="text" id="email" name="email"></p>

<p>Message:
<textarea id="message" name="message"></textarea></p>

Use this html markups. It has ids which you have missed. You are using document.getElementById("name"). But the input field doesn't have id "name". Same for other fields.

Upvotes: 0

Saurabh Singhal
Saurabh Singhal

Reputation: 281

Instead .. add the validation on the form as below:

<form method="post" name="contact_form" action="contact-form-handler.php" 
onsubmit="return validateForm()">

This works 100% times

Upvotes: 1

Emilio Rodriguez
Emilio Rodriguez

Reputation: 5749

try this instead for your button:

<input type="button" value="submit" onclick="function(){ check(document.getElementById('name').value,document.getElementById('email').value,document.getElementById('message').value)}" /> 

Upvotes: 0

Related Questions