I got the form checking if fields are empty now using javascript but now the form is not sending the email where as it was before
the updated code is below
Code:
<form name="form" method="post" action="index.php#contact" onsubmit="return checkForm(this);">
<input class="input-text" type="text" name="name" id="name" value="Your Name *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" required>
<input class="input-text" type="text" name="email" id="email" value="Your E-mail *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" required>
<input class="input-text" type="text" name="phone" id="phone" value="Your Phone Number *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" required>
<textarea class="input-text text-area" cols="0" rows="0" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" name="message" id="message" required>Your Message *</textarea>
<p>
<img src="captcha.php" width="120" height="30" border="1" alt="CAPTCHA">
</p>
<p>
<input type="text" size="6" maxlength="5" name="captcha" value="">
<br>
<small>copy the digits from the image into this box</small>
</p>
<input class="input-btn" type="submit" value="send message">
</form>
<script>
function checkForm(form)
{
if(!form.captcha.value.match(/^\d{5}$/)) {
alert('Please enter the CAPTCHA digits in the box provided');
form.captcha.focus();
return false;
}
return true;
}
window.onload = function (event) {
var form = document.getElementsByName('form')[0];
form.addEventListener('submit', function (event) {
var inputs = form.getElementsByTagName('input'), input, i;
for (i = 0; i < inputs.length; i += 1) {
input = inputs[i];
if (input.type === 'text' && input.value.trim() === '') {
event.preventDefault();
alert('You have empty fields remaining.');
return false;
}
}
}, false);
};
</script>
Bookmarks