1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> Email Address: <input type='text' id='myemail'/><br/> <input type='submit' class='save' Value='Validate Email' /> <script> $(document).ready(function(e) { $('.save').click(function() { var sEmail = $('#myemail').val(); if ($.trim(sEmail).length == 0) { alert('Please enter valid email address'); e.preventDefault(); } if (validateEmail(sEmail)) { alert('Email is valid'); } else { alert('Invalid Email Address'); e.preventDefault(); } }); }); function validateEmail(sEmail) { var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; if (filter.test(sEmail)) { return true; } else { return false; } } </script> |
Basic code
Add this to your input
1 2 3 |
onblur="checkEmail()" |
1 2 3 4 5 6 7 8 9 10 11 12 |
<script language="javascript"> function checkEmail() { var email = document.getElementById('emailaddress'); var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/; if (!filter.test(email.value)) { alert('Please provide a valid email address'); email.focus(); return false; }} </script> |
Any field with an id of emailaddress will be checked if this function is called.
Alternative
1 2 3 4 5 6 7 8 9 |
// Check Valid or Invalid email when user enters email in email input field. if ($formfield == "email") { if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $value)) { echo "Invalid email"; } else { echo "<span>Valid</span>"; } |