jquery - How to validate email live using javascript and change textbox background color -


my java code:

<script type='text/javascript'> $(document).ready(function () {      $('#email').keyup(function() {               var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-za-z\-0-9]+\.)+[a-za-z]{2,}))$/;                if (re.test($(this).val())) {                      $(this).css("background-color", "green");                } else {                      $(this).css("background-color", "red");               }         }); }); </script> 

my html:

<p><b>email address:</b> <input type="text" name="email_address" id="email" /> </p> 

i looking live-update type of effect user types, not click-away affect.

nothing seems work, have used similar code change values of other text-boxes in live-fashion. sorry if repeated question.

thank you!

your example working fine on computer.
sure have included jquery? version? browser using?
working code:

<!doctype html> <html>  <head>     <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> </head>  <body>     <script type='text/javascript'>         $(document).ready(function () {             $('#email').keyup(function () {                 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-za-z\-0-9]+\.)+[a-za-z]{2,}))$/;                  if (re.test($(this).val())) {                      $(this).css("background-color", "green");                  } else {                      $(this).css("background-color", "red");                 }             });         });     </script>     <p><b>email address:</b>          <input type="text" name="email_address" id="email" />     </p> </body>  </html> 

and result:

enter image description here

edit: if want easy client validation library suggest using jquery validation


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -