javascript - How to check if my form is valid after click on `add_button` and if is not than change color of input/select? -


html:

<select id="id_myselect" name="myselect">    <option value="0">---------</option>    <option value="1">1</option>    <option value="7">2</option>    <option value="10">3</option> </select>  <select id="id_book" name="book">    <option value="0">---------</option>    <option value="1">book1</option> </select>  <input id="id_number" name="number" type="number" /> <div id="add_button">add</div> 

how check if form valid after click on add_button , if not change color of input/select?

my try jquery:

if($( "#id_myselect option:selected" ).val() == 0){        $("#id_myselect").addclass( "error" );     } else if($( "#id_book option:selected" ).val() == 0){        $("#id_book").addclass( "error" ); }      else if($( "id_number" ).val() <= 0){        $("#id_number").addclass( "error" ); } 

onclick of add_button check $('.error').length. if 0 there no error:

 $('#add_button').on('click',function(){      if($('.error').length!=0){         console.log('error exist');      }  }); 

and when comes changing color, add css .error class.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

angularjs - ng-repeat duplicating items after page reload -