javascript - jQuery - building dynamic if statements based on object passed as the function argument -


i'm trying build several dynamic if statements based on definition in argument of function. should able run them if particular keys , vales provided. can read keys , values, not sure how build code upon them. object passed function argument:

param = {     'fields': {             'email' : {             'match' : 'email'         },         'countadults': {             'match' : 'number',             'range' : '1, 10'         }     } }; 

//and bit trying parse object

$.each(param.fields, function(key, value){ // reading definitions parameter     if(name == "'+key+'") $('[name="'+key+'"]').mandatory(); // define begining of if     $.each(param.fields[key], function(subk, subv){                                 += '.'+subk+'("'+subv+'")'; // adding more if statement     });     });  return (all if statement); } 

after returning of these if statements run else statement default cases. i'm trying move these bits of code body of main function, place call function, wouldn't have customize body of function each time.

i suggest instead have elements add class every validation want have. this:

<!doctype html> <html>   <head>     <style type="text/css">       input.invalid { border: 1px solid red; }     </style>     <script       src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js">     </script>      <script>       $(function()       {         $('.email').each(function()         {           var input = $(this);            input.keyup(function()           {             validate_as_email(input);           });         });       });        function validate_as_email(input)       {         var value = input.val();          if (is_email(value))           input.removeclass('invalid');         else           input.addclass('invalid');       }        function is_email(value)       {         return value.match           (/^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i) != null;       }     </script>   </head>   <body>     email:<br>     <input type="text" id="email" class="email">   </body> </html> 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -