-1

I want to check on form submit that certain condition is satisfied before submitting the form

function checkConditions(e){
    if ($('#someId').val() == '') {
       console.log('value of #someId is empty');        
       e.preventDefault();
       return false;
    }
}
$('form').submit(function (e) {            
     checkConditions(e);    
});

on form submit I'm getting print inside console that value is empty but form is sumbitted eitherway. What I'm doing wrong here?

user1765862
  • 13,635
  • 28
  • 115
  • 220
  • That should work. [That works.](http://jsfiddle.net/ezvwg42b/) – T.J. Crowder Nov 26 '15 at 11:15
  • use e.preventDefault(); – Yuri Nov 26 '15 at 11:15
  • @Yuri: Look again at the question. – T.J. Crowder Nov 26 '15 at 11:18
  • Why not use the built in features of MVC and apply the `[Required]` attribute to the property and enable client side validation? –  Nov 26 '15 at 11:18
  • @StephenMuecke :) I'm aware of that but I have my own reasons for doing this way. – user1765862 Nov 26 '15 at 11:19
  • maybe you need $.trim($('#someId').val()) == '' – Mohamed-Yousef Nov 26 '15 at 11:28
  • @Mohamed-Yousef: That would make sense, but the OP says that he sees the console.log... – T.J. Crowder Nov 26 '15 at 11:36
  • why is this downvoted? – user1765862 Nov 26 '15 at 11:42
  • @T.J.Crowder you are right .. but for me sometimes I can't find a noticed deference between empty and spaces in console.log or alert .cause of that I said about trim to avoid right and left whitespaces . anyway trim will help him somewhere :-) .. – Mohamed-Yousef Nov 26 '15 at 11:43
  • 1
    @user1765862: From the button's tooltip: "...is is unclear or not useful." The code in the question works, as was pointed out (and proven) half an hour ago. So the question cannot be answered as it currently stands. Eventually, not having been clarified or removed, it will attract down- and close-votes. Fortunately, Joseph was good enough to remove his incorrect (but upvoted) answer, so you can delete the question if you can't add more detail to it. – T.J. Crowder Nov 26 '15 at 11:45
  • 1
    @T.J.Crowder is right, I was in the understanding that a return would work, but in effect it's doing the same. It is something that I would love to know how it got solved in the end though. – Joseph Callaars Nov 26 '15 at 11:47
  • Are you using jQuery mobile? If yes, try to add `data-ajax="false"` in your `
    `
    – Renan Araújo Nov 26 '15 at 11:49
  • @Joseph: Congrats on your new [disciplined badge](http://stackoverflow.com/help/badges/37/disciplined). You are one of an elite few who are disciplined enough to do the correct thing, nice one! – T.J. Crowder Nov 26 '15 at 11:51
  • Sorry I was a bit stubborn there ;) – Joseph Callaars Nov 26 '15 at 11:52

1 Answers1

0

Try this:

function checkConditions(){
    if ($('#someId').val() == '') {
       console.log('value of #someId is empty'); 
       return false;
    }
    return true;
}
$('form').submit(function (e) {            
     if(!checkConditions()){
         e.preventDefault();
         return false;
     }
});
Rajesh Kumar
  • 471
  • 5
  • 14