I have used regex to test for the email input. It does give an alert when a wrong email (for eg without an @ or .com) is submitted but when I press ok for the alert, it still posts the email to the email address that is am directed to the page send_msg.php when it should not.. I am quite new to Javascript and PHP so I am still learning.. I know that the if clause should follow an else but I don't know how to make it post the email if it is true. And can how I make it just display a message below the email input field instead of making it raise an alert?
<script>
function checkEmail(inputvalue){
var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
if(pattern.test!=(inputvalue)){
alert("Please enter a valid email address");
return false;
}
}
<form id="contact-form" name= "form1 "action="send_msg.php" method="post">
<h3>Get in touch</h3>
<h4>Fill in the form below, and we'll get back to you within 24 hours.</h4>
<div>
<label>
<span>Name: </span>
<input placeholder="Please enter your name" name="name" type="text" tabindex="1" required autofocus>
</label>
</div>
<div>
<label>
<span>Email: </span>
<input placeholder="Please enter your email address" name="email" type="email" tabindex="2" required>
</label>
</div>
<div>
<label>
<span >Message: </span>
<textarea placeholder="Include all the details you can" name="message" tabindex="5" size= "35" maxlength= "255"></textarea>
</label>
</div>
<div>
<button name="submit" type="submit" onClick="checkEmail(document.contact-form.email.value)" id="contact-submit">Send Email</button>
</div>
</form>