1

I made this input form with Name and Text.

<form action="" method="post" class="main">
    <label>Write a comment!</label><br>
    <input placeholder="Name" class="form-text" name="user" type = "text" id = "user" autofocus size = "48"><br/>
    <textarea class="form-text" name="comment" id="comment" placeholder="Text"></textarea>
    <br />
    <input type="submit" class="form-submit" name="new_comment" value="Submit comment">
</form>

and I added some jQuery for this form.

$(".form-submit").click(function() {
    var commentBox = $("#comment");
    var userBox = $("#user");

    var commentCheck = commentBox.val();
    var userCheck = userBox.val();

    if(commentCheck == '' || commentCheck == NULL ) {
        commentBox.addClass("form-text-error");
        console.log(commentBox);
        return false;
    }   

    if (userCheck == '' || userCheck == NULL){
        userBox.addClass("form-text-error");
        console.log(userBox);
        return false;
    }
});

And now I'm here with this problem. I want the user to fill both fields in order to write a comment (name & text). For any empty fields I want to add class "form-text-error" . Everything works except for field with users name.

Any suggestions?

1
  • "I want the user to fill both fields in order to write a comment" You don't have two fields which you will fill to allow the comment, you just have a username and a comment fields. Or you meant to say "in order to POST ...." Commented Dec 10, 2017 at 10:55

5 Answers 5

1

Use Length to get length of input then if return zero do addClass

$(".form-submit").click(function() {
  var commentBox = $("#comment");
  var userBox = $("#user");

  var commentCheck = commentBox.val();
  var userCheck = userBox.val();



  if (commentCheck.length <= 0 || commentCheck == NULL) {
    commentBox.addClass("form-text-error");
    console.log(commentBox);
    return false;
  }

  if (userCheck.length <= 0 || userCheck == NULL) {
    userBox.addClass("form-text-error");
    console.log(userBox);
    return false;
  }



});
.form-text-error {
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" class="main">
  <label>Write a comment!</label><br>
  <input placeholder="Name" class="form-text" name="user" type="text" id="user" autofocus size="48"><br/>
  <textarea class="form-text" name="comment" id="comment" placeholder="Text"></textarea>
  <br />
  <input type="submit" class="form-submit" name="new_comment" value="Submit comment">
</form>

Also you can use submit function instead of click:

$(".form-submit").submit(function() {});

But i recommend you to use something like this:

$(".form-submit").click(function(e) {
  e.preventDefault(); //remove this later
  $('.form-text').each(function() {
    if ($(this).val().length <= 0) {
      $(this).addClass('form-text-error');
      return false;
    } else {
      $(this).removeClass('form-text-error');
      return true;
    }
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your code, but now i can send the form with both fields empty :D
@TomsEglite return false cause of this. try my rewrited code, and let me know the result.
0

it is due to condition if return with false so it will check 1st condition and return back no further check. need to make changes accordingly as below.

$(".form-submit").click(function() {
    var commentBox = $("#comment");
    var userBox = $("#user");

    var commentCheck = commentBox.val();
    var userCheck = userBox.val();
    var err = false;


    if(commentCheck == '' || commentCheck == NULL ) {
        commentBox.addClass("form-text-error");
        console.log(commentBox);
        err = true;
    }   

    if (userCheck == '' || userCheck == NULL){
        userBox.addClass("form-text-error");
        console.log(userBox);
        err = true;
    }
if(err)
return false;


});

2 Comments

Still the same thing happens.
Ok, sorry, I did not check the code properly. Now it kinda works, i managed to add form-error-class to both fields, but anyway, i can send the form even with one field empty.
0

You are doing return false after comment

if(commentCheck == '' || commentCheck == NULL ) {
        commentBox.addClass("form-text-error");
        console.log(commentBox);
        return false;
    } 

That's why it didn't get name field You can use like this as like your requirement,

var commentBox = $("#comment");
var userBox = $("#user");
var commentCheck = commentBox.val();
var userCheck = userBox.val();
var flag =0;
if(commentCheck == '' || commentCheck == NULL ) {
    $("#comment").addClass("form-text-error");
    console.log(commentBox);
    flag = 1;
}   
if (userCheck == '' || userCheck == NULL){
    $("#user").addClass("form-text-error");
    console.log(userBox);
    flag = 1;
}
if(flag == 1)return false;  

3 Comments

This is the same solution as @Komal suggested me to do. Now I can add a class to both fields if they are empty, but I still can send the form even if one field is empty.
Then use this snippet, it will return false for any empty input fields.. var commentBox = $("#comment"); var userBox = $("#user"); var commentCheck = commentBox.val(); var userCheck = userBox.val(); var flag =0; var flag1 =0; if(commentCheck == '' || commentCheck == NULL ) { $("#comment").addClass("form-text-error"); console.log(commentBox); flag = 1; } if (userCheck == '' || userCheck == NULL){ $("#user").addClass("form-text-error"); console.log(userBox); flag1 = 1; } if(flag == 1 || flag1==1)return false;
Even after this code I can still send the form even with one empty field.. this is pain in the ass. :D
0
Use input type="button" and use this snippet that's tested and it works...
$("#comment").removeClass("form-text-error");
        $("#user").removeClass("form-text-error");
        var commentBox = $("#comment");
        var userBox = $("#user");
        var commentCheck = commentBox.val();
        var userCheck = userBox.val();
        var flag =0;
        var flag1 =0;
        if(commentCheck == '' || commentCheck == null ) {
            $("#comment").addClass("form-text-error");
            console.log(commentBox);
            flag = 1;
        }   
        if (userCheck == '' || userCheck == null){
            $("#user").addClass("form-text-error");
            console.log(userBox);
            flag1 = 1;
        }
        if(flag == 1 || flag1 == 1){return false;}
        else{return true;}

Comments

0

You are returning false for both the validations. Also, use .length === 0 || !box. A common function would help.

Fixed

use e.preventDefault(); This would validate the username field as well and would not submit if empty.

var commentBox = $("#comment");
var userBox = $("#user");
   
function checkIfEmpty(box, check) { 
    if (check.length === 0  || !box) {
        box.addClass("form-text-error");
        console.log(box);
        return true;
    } else {
        box.removeClass("form-text-error");
        return false;
    }
 }
    
$(".form-submit").on('click', function(e) {
    e.preventDefault();
    var commentCheck = commentBox.val();
    var userCheck = userBox.val();
    var commentCall = checkIfEmpty(commentBox, commentCheck);
    var userCall =  checkIfEmpty(userBox, userCheck);
    if(userCall === true && commentCall === true) {
      return false;
    }
});
   
.form-text-error {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" class="main">
    <label>Write a comment!</label>
    <br/>
    <input placeholder="Name" class="form-text" name="user" type="text" id="user" autofocus size="48"/>
    <br/>
    <textarea class="form-text" name="comment" id="comment" placeholder="Text"></textarea>
    <br/>
    <input type="submit" class="form-submit" name="new_comment" value="Submit comment"/>
</form>

4 Comments

It adds the text error class, but i can still send the input to my db with one field empty. I' m starting to think that there could be a problem somewhere else.
Fixed the code. Now, the form won't submit if name field is empty.
Copied the code, it gets this error: Uncaught TypeError: Cannot read property 'length' of undefined at checkIfEmpty (global.js:14) at HTMLInputElement.<anonymous> (global.js:29) at HTMLInputElement.dispatch (jquery.min.js:3) at HTMLInputElement.r.handle (jquery.min.js:3)
The snippet is working fine. I think it's related to the variable scope. Try declaring var commentBox = $("#comment"); var userBox = $("#user"); variables as global.

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.