1

I want to make it so that in my form the user cannot click the submit button until every field has an option selected. I originally tried this with a while loop and it created an infinite loop. I converted it to an if statement and added code to call the if statement every time something on the form is changed, but still the submit button never becomes clickable.

DEMO

if(($('input[type=text]').val() === '') || (!($('input[name=class]').is(':checked'))) || (!($('input[name=race]').is(':checked')))) {
    $(' input[type=submit]').css('pointer-events', 'none');
}
else{
    $(' input[type=submit]').css('pointer-events', 'auto');
}

$('input[name=user]', 'input[name=race]', 'input[name=class]').change(function(){
    if(($('input[type=text]').val() === '') || (!($('input[name=class]').is(':checked'))) || (!($('input[name=race]').is(':checked')))) {
        $(' input[type=submit]').css('pointer-events', 'none');
    }
    else{
        $(' input[type=submit]').css('pointer-events', 'auto');
    }
});
Shniper
  • 854
  • 1
  • 9
  • 31

5 Answers5

1

why not use required attribute ? Here is a link http://www.w3schools.com/tags/att_input_required.asp http://www.w3schools.com/tags/att_select_required.asp

If you want more sophisticated validation, take a look at this https://jqueryvalidation.org/

Hammer
  • 8,538
  • 12
  • 44
  • 75
1

Multiple selectors should we wrapped in a quote and separated by comma(,)

jQuery( "selector1, selector2, selectorN" )

Note: Also wrap value of the attribute in quotes(")

jQuery( "[attribute='value']" )

if (($('input[type="text"]').val() === '') || (!($('input[name="class"]').is(':checked'))) || (!($('input[name="race"]').is(':checked')))) {
  $(' input[type="submit"]').css('pointer-events', 'none');
} else {
  $(' input[type="submit"]').css('pointer-events', '');
}
$('input[name="user"],input[name="race"],input[name="class"]').change(function() {
  if (($('input[type="text"]').val() === '') || (!($('input[name="class"]').is(':checked'))) || (!($('input[name="race"]').is(':checked')))) {
    $(' input[type="submit"]').css('pointer-events', 'none');
  } else {
    $(' input[type="submit"]').css('pointer-events', '');
  }
});
#newPlayer {
  position: relative;
  top: 20px;
  color: #8b0909;
  background-color: rgba(0, 0, 0, .3);
  width: 350px;
  margin: auto;
  height: 275px;
  border-radius: 5px;
  text-shadow: 1px 1px 1px black;
}
#newPlayer h3 {
  font-size: 20px;
  font-family: 'Droid Sans', sans-serif;
  font-weight: 700;
  padding-bottom: 5px;
}
#newPlayer p {
  max-width: 275px;
  margin: auto;
  margin-top: 5px;
  color: #c10606;
  font-size: 14px;
  font-family: 'Open Sans', sans-serif;
  font-weight: 300;
}
#newPlayer input[type=submit] {
  color: black;
  border-radius: 5px;
  font-family: 'Droid Sans', sans-serif;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form id="newPlayer">
  <h3>Username:</h3>
  <input type="text" name="user" />
  <br>
  <h3>Please Choose a Class:</h3>
  <input type="radio" name="class" value="archer" />Archer
  <input type="radio" name="class" value="mage" />Mage
  <input type="radio" name="class" value="warrior" />Warrior
  <br>
  <h3>Please Choose a Race:</h3>
  <input type="radio" name="race" value="Orc" />Orc
  <input type="radio" name="race" value="Elf" />Elf
  <input type="radio" name="race" value="Human" />Human
  <input type="radio" name="race" value="Worg" />Worg
  <br>
  <input type="submit" value="Submit" />
  <p id="descript"></p>
</form>
Rayon
  • 36,219
  • 4
  • 49
  • 76
1

A better and shorter way to accomplish your task.

$('form#newPlayer').submit(function(){ // bind form submit event
   if($('input:text').val() == "" || !$('input[name="class"]:checked').length || !$('input[name="race"]:checked').length)
   {
     alert("all fields required");
     return false; // prevent form submission.
   }
});

DEMO

Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
0

Write validate function which return true if all conditions are met or false if not. Then on click run the function and if it returns true then do form.submit()

function validateData() {
  
  var result = true;
  
  if(!$('.yourinput1').val()) {
    result = false;
  }
    
   if(!$('.yourinput2').val()) {
    result = false;
  }
     
  
  return result;
  
  }



$('#yourbutton').click(function(e){
  
  e.preventDefault();

  if(validateData()) {
    
    $('#yourForm').submit();
  
  }


});
Nick Surmanidze
  • 1,671
  • 1
  • 11
  • 20
0

From problem statement , it seems you are looking to disable the submit during initial page load and also if all fields are not selected & enable it only if all fields are selected.

So if it the case you need to initially disable submit button.

For that you can use disabled attribute.

Here is the change I did

HTML

<input type="submit" value="Submit" disabled="true" class = "disableSubmit"/>

CSS

.disableSubmit{
  cursor:no-drop; // this will let user know button cannot be selected
}

JS

// function which will validate every field
function _validateFields(){
  var _textField = $('input[type=text]').val();
  var _secondField = $('input[name=class]').is(":checked");
  var _thirdField = $('input[name=race]').is(":checked");
  if((_textField === undefined || _textField ==="") || _secondField == false || _thirdField == false){
    return false;
   } 
  else{
    return true;
  }
}


$('input[name="user"],input[name="race"],input[name="class"]').on('change keyup',function() {
  if(_validateFields()){
    $(' input[type="submit"]').attr("disabled",false).removeClass('disableSubmit');
  }
  else{
   $('input[type="submit"]').attr("disabled",true).addClass('disableSubmit');
  }
})

Plunker for demo

brk
  • 48,835
  • 10
  • 56
  • 78