I have a form that looks like this
<form id="form-send">
<table class="form-table">
<tbody>
<tr>
<td width="30%">First Name <span class="required">*</span></td>
<td width="70%">
<input type="text" id="first-name" value="" />
<div id="first-name-error-msg" class="error-box"></div>
</td>
</tr>
<tr>
<td>Last Name<span class="required">*</span></td>
<td>
<input type="text" id="last-name" value="" />
<div id="last-name-error-msg" class="error-box"></div>
</td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" value="" /></td>
</tr>
<tr>
<td><button type="submit">Send</button></td>
<td><button type="reset">Reset</button></td>
</tr>
</tbody>
</table>
</form>
The script I use to validate the form inputs looks like this
$(document).ready(function($) {
$('#form-send').submit(function (){
var error = 0;
var name = $('#first-name').val();
if (name == '') {
error = 1;
$('#first-name').addClass('error');
$('#first-name-error-msg').html(" - First name is required");
$('#first-name-error-msg').parent().show();
}
var name = $('#last-name').val();
if (name == '') {
error = 1;
$('#last-name').addClass('error');
$('#last-name-error-msg').html(" - Last name is required");
$('#last-name-error-msg').parent().show();
}
if (error) {
return false;
} else {
return alert("Email sent");
}
});
});
If I press the submit button without filling the fields the error messages are displayed. What I am trying to do is: after the display of the error messages when the user starts to type again into the input field, the error message, the div that contains the message, should disappear. I have tried something like this
$('.error').keyup(function() {
$('.error-box').hide('slow');
});
but it won't work, can someone give me some hints on what am I doing wrong or what other method should I use ?