0

I want to not submit the form if the inputs are empty, here is my code:

 <html>
<head>
<title>
The X/O Game
</title>
<script type="text/javascript">
var check = function () {
    var x = document.getElementById("x").value;
    var o = document.getElementById("o").value;
    var p = document.getElementById("p").value;

    if(p==""||(x==""&&o=="")){
        alert("fill the form!");
        return false;
    }

    return true;
};

$('#formm').submit(function(e){
    var shouldSubmit = check();

    if (!shouldSubmit) {
        e.preventDefault();
    }                                    
});

$('#emotion input:radio').addClass('input_hidden');
$('#emotion label').click(function(){
    $(this).addClass('selected').siblings().removeClass('selected');
});
</script>
<link rel="stylesheet" type="text/css" href="style/style.css" />
<body>
<div>
Please enter your name & choose your character before start playing:
</div>
<div>
<form method=post action=game.php name="formm">
Name:<br>
<input type=text name=player id=p>
</div>
<div>
Character:<br>
<input 
  type="radio" name="emotion" value="xChar"
  id="x" class="input-hidden" />
<label for="x">
  <img src="images/x.png " />
</label>
<input 
  type="radio" name="emotion" value="oChar"
  id="o" class="input-hidden" />
<label for="o">
  <img src="images/o.png" />
</label>
</div>
<div>
<input type=submit value=Play>
</form>
</div>
</body>
</html>

    $('#formm').submit(function(){
        return  f;                                       
    });

this function is called when the user clicks on the submit button. the form is subbmited even though the inputs are empty, where is the wrong?

1
  • Try to not change the question, then you will falsify answers Commented Nov 22, 2013 at 19:29

5 Answers 5

1

f is defined when you call check(), they will not magically update. Do the checks inside the submit function.

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

Comments

1

You'd better use HTML5 required attribute here:

<form>
  <input type="text" name="x" required>
  <input type="submit">
</form>

If you want more complex validation, you should have a look at html5rocks.com. The form validation should move from Javascript to HTML now (or in the near future).

But if you want to do it your way, do as epascarello suggests here:

$('#formm').submit(function(){
    check();
    return  f;                                       
});

1 Comment

This is a great suggestion, though his logic appears to be a little more verbose than just required (p==""||(x==""&&o==""))
0

Try this:

$('#yoursubmitbtnid').click(function(){
    var x = document.getElementById("x").value;
    var o = document.getElementById("o").value;
    var p = document.getElementById("p").value;

    if(p==""||(x=="" && o=="")){
        alert("fill the form!");
        return false;
    }                                     
});

1 Comment

be careful not to return false when you mean to just stop the event default behavior. this will break bubbling and event delegation.
0

You need to run the check function in the submit handler to determine whether or not the submit should be allowed.

var check = function () {
    var x = document.getElementById("x").value;
    var o = document.getElementById("o").value;
    var p = document.getElementById("p").value;

    if(p==""||(x==""&&o=="")){
        alert("fill the form!");
        return false;
    }

    return true;
};

$('#formm').submit(function(e){
    var shouldSubmit = check();

    if (!shouldSubmit) {
        e.preventDefault();
    }                                    
});

You may want to look into using a validation plugin (such as this) if you plan on doing any extensive client side validation.

2 Comments

it seems as the most suitable answer, but its not working either :( , i'll post the all code of my page.
@user2996426 you're binding event handlers to elements before they exist. HTML in parsed only once from top to bottom. Put your jquery code inside of a $(document).ready() or move it to the bottom of the body tag.
0

You need to call the check function when the form is being submitted

$('#formm').submit(function(){
    return check();                                       
});

If the check function returns false then the form should not submit.

Comments

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.