In this post i want to explain how to Disable submit button until form fields are filled using jQuery.This jQuery is used to check all form fields are filled are not.If fields are not filled returns false,otherwise return true.keyup() function is used to in this example.
Before that we need to include jQuery Library file.Copy and Paste the following URL.Library File: <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
jQuery Function
<script type="text/javascript">
(function() {
$('form > input,textarea').keyup(function() {
var empty = false;
$('form > input,textarea').each(function() { //Check form fields are empty or filled
if ($(this).val() == '') {
empty = true; //result
}
});
if (empty) {
$('#send').attr('disabled', 'disabled');
} else {
$('#send').removeAttr('disabled');//remove disabled
}
});
})()
</script>
(function() {
$('form > input,textarea').keyup(function() {
var empty = false;
$('form > input,textarea').each(function() { //Check form fields are empty or filled
if ($(this).val() == '') {
empty = true; //result
}
});
if (empty) {
$('#send').attr('disabled', 'disabled');
} else {
$('#send').removeAttr('disabled');//remove disabled
}
});
})()
</script>
HTML File
<form name="register" action="#" method="post">
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Username</td>
<td><input type="text" id="user_input" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td> Message</td>
<td><textarea name="message"></textarea></td>
</tr>
</table>
<input type="submit" id="send" value="Send" disabled="disabled" />
</form>
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Username</td>
<td><input type="text" id="user_input" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td> Message</td>
<td><textarea name="message"></textarea></td>
</tr>
</table>
<input type="submit" id="send" value="Send" disabled="disabled" />
</form>
No comments:
Post a Comment