0

I am new to jquery. I am trying to create a form in which if the user does not enter username the user will get a error. I have created a form which has input text, a span tag and a button. If i click on the button and the username field, the span tag should be visible.

My HTML code looks like:

<head>
<link rel="stylesheet" type="text/css" href="style.css">
  <script src="jquery-1.9.1.min.js"></script>
<script src="check1.js"></script> 

<body>      
  <div>
    <form id="myform" method="post">
            <table border="1">
              <tr>
                <td>
                  <input type="text" id="uname"/>
                </td>
                <td>
                  <span class="error">please enter data</span>
                </td>
              </tr>
              <tr>
                <td>
                  <button id="check"> submit</button>                      
                </td>
              </tr>
            </table>
    </form>
  </div>                
</body>

My style.css file:

.error
{
    visibility: hidden;
}

.error_show
{
    visibility: visible;
    color: red;

}

And the JQuery file:

$(document).ready(function() {
    $("#myform").on("submit", function(){
        var uname = $("#uname").val();
        if(uname == "")
        {
            $(".error").addClass("error_show");
        }
    });

});

I have used form submit in JQuery. The error gets diaplayed for less than a second. Why is that happening? Is there anything wrong in the code?

2
  • 1
    Use return false; or event.preventDefault(); in if Commented Dec 2, 2015 at 4:41
  • because form is getting submitted.nothing is wrong in your code it behaves like it should Commented Dec 2, 2015 at 4:41

2 Answers 2

2

disable the default behaviour of submit event with preventDefault()

  $("#myform").on("submit", function(e){
        e.preventDefault();
          ....rest of your code     
    });
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot! it worked. Can you explain what exactly does e.preventDefault(); does?
@AniruddhJhavar it cancels out the default behaviour of any event developer.mozilla.org/en/docs/Web/API/Event/preventDefault
1

You should do you validation on the click event of the submit button, then if it passes call submit on the form itself.

Also, notice the e.preventDefault(). This prevents the form from submitting before you tell it to.

$("#check").click(function(e) {
    e.preventDefault();
    var uname = $("#uname").val();
    if(uname == "")
    {
        $(".error").addClass("error_show");
    } else {
        $("#myform").submit();
    }
});

2 Comments

Is form on submit not preferred or is there any other reason why you used button click?
This is really the main reason, to do validation before the form is submitted, while in your method the form has been submitted and you need to cancel it if validation fails. Better to check first.

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.