JSFiddle - React, Tailwind, and code Playground

by nimeshrmr

HTML

<form id='targets' action='http://www.innovativephp.com' method='post'  >    
     <input type='text' name='test_field' id='name' />
    <br/>
     <select name='test_field'  id='country' >
         <option value='0'>Select</option>
         <option value='LK'>Sri Lanka</option>
         <option value='IN'>India</option>
         <option value='UK'>United Kingdom</option>
     </select>
     <br/>
     <textarea name='test_field'  id='comment' ></textarea>
     <br/>
     <input type='checkbox' name='test_field'  id='checkboxid' />
    <br/>
     <input type='submit' value='Save' />
 </form>

JavaScript

$('#targets').submit(function() {
  var error=0;
    
    var name = $('#name').val();
    if(name == ''){
        error = 1;
        alert('Name cannot be empty');
    }
    
    
    var country = $('#country').val();
    if(country == '0'){
        error = 1;
        alert('You should select a country.');
    }
    
    var comment = $('#comment').val();
    if(comment == ''){
        error = 1;
        alert('Comment cannot be empty.');
    }
    
    if(!($('#checkboxid').is(':checked'))) {
        error = 1
        alert("Please Tick the Agree to Terms of Use");
    }
    
    if(error){
        return false;
    }else{
        return true;
    }       
});