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?
return false;orevent.preventDefault();inif