1

This might be simple but I'm having difficulties here. I'm running the required attribute through all fields of .um_frm and then checking if all fields are not empty. My problem is that instead of checking ALL fields, it passes through even if a single field is filled. How do I make sure that all fields are filled?

$('#um_ok').on( 'click', function() {
      $('.um_frm').attr('required','required'); 
      var value=$.trim($(".um_frm").val()); 
      if(value.length === 0){
          //proceed...
      }
});

I also tried this but was not suiccessful

$('#um_ok').on( 'click', function() {
       $('.um_frm').attr('required','required'); 
       if($.trim($(".um_frm").val()) === ""){
           //proceed...
       }
 });
1
  • 1
    Please make a jsfiddle. Commented Oct 15, 2014 at 9:32

3 Answers 3

4

Use a filter to see if any field is blank (length not truthy, i.e. 0, hence the !):

$('#um_ok').on( 'click', function() {
      $('.um_frm').attr('required','required'); 

      var anyBlank = $(".um_frm").filter(function(){
          return !$.trim($(this).val()).length;
      }).length; 

      // If not any blanks...
      if(!anyBlank){
          //proceed...
      }
});

Update: (thanks @George)

As required is a genuine HTML element property and not just an attribute, you should use prop (which can then take the more readable Boolean value as its on/off state):

      $('.um_frm').prop('required', true); 

This has the advantage of creating the cleaner required attribute with no value (instead of required="required" etc).

Sign up to request clarification or add additional context in comments.

8 Comments

+1 An up-vote for an answer that actually deserves one. Make a note about using .prop() to set properties like required (:
@TrueBlueAussie I have no reference except this question. If you think logically, we don't care about the required attribute, we want to change the property because that is what causes required's behaviour.
@TrueBlueAussie The code does not run when I use prop.
@George: hmm. Is that a browser compatibility thing?
I tried this using the latest version of chrome (v38)
|
1

WORKING FIDDLE

Use following

$('#um_ok').on('click', function () {
    var status = true;
    $('.um_frm').each(function () {
        if ($.trim($(this).val()) == '') {
            status = false;
        }
    });
    if (status) {
        alert("all r ok");
    } else {
        alert("sumthng missing");
    }
});

5 Comments

This will alert/action once for every blank entry (quite annoying)... Better to maybe return an overall result and then report it?
Better now (and I formatted it for you), but I would suggest using string values for a boolean state is not an appropriate thing to encourage. Just use true & false :)
@TrueBlueAussie did. thnx
+1: Got there in the end :) I still prefer filter to each, but the end result is the same).
@Fergoso , it should ! Thanks
0

Hope this will do

var left_blank =$('#um_ok').on( 'click', function() {
                    $(".um_frm").filter(function () {
                      return !$.trim($(this).val()).length;
                  }).length;
               });
if(!left_blank){
    //proceed
}

7 Comments

Wait, how does it answer question?
You should either add the appropriate if statement afterwards, or explain to the OP what is going on here. As it stands, this code does nothing except evaluate a true or false value...
You are not actually checking for blanks... you are checking for any non-blanks at the moment.
I missed it actually :)
@BlankHead Your if statement will be carried out not when the button is clicked, but straight away, and never again. Also you need left_blank to have the value of the length property you get. You are assigning the jQuery object that you are attaching the handler to.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.