php - Javascript regex email validation -
this question has answer here:
- how validate email address in javascript? 65 answers
i have used regex test email input. give alert when wrong email (for eg without @ or .com) submitted when press ok alert, still posts email email address directed page send_msg.php when should not.. quite new javascript , php still learning.. know if clause should follow else don't know how make post email if true. , can how make display message below email input field instead of making raise 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 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 form below, , we'll within 24 hours.</h4> <div> <label> <span>name: </span> <input placeholder="please enter name" name="name" type="text" tabindex="1" required autofocus> </label> </div> <div> <label> <span>email: </span> <input placeholder="please enter email address" name="email" type="email" tabindex="2" required> </label> </div> <div> <label> <span >message: </span> <textarea placeholder="include details 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>
assuming regex validation ok, can prevent form submit, doing this:
<form onsubmit="return checkemail(document.contact-form.email.value);" id="contact-form" name= "form1 "action="send_msg.php" method="post"> ... </form>
and remove onclick
submit button. if regex function returns true if valid , false if not valid, form submit when email input has valid data in it.
hope helps.
let me know.
cheers!
Comments
Post a Comment