I use Jquery validation plug-in to validate these two input elements below.
When an input is invalid the plug-in will generate a label tag right after the input field, such as: <label for="company" style="">Required !</label>
Question:
How do I put the error message in the existed <span class="help-inline">
instead of generating a new <label>
?
Many thanks for your help in advance.
<script type="text/javascript">
$('#signUpForm').validate({
debug: false,
rules: {company:"required",username:"required"},
messages: {company:"Required !",username:"Required !"}
})// End of validate()
</script>
<div class="control-group">
<label class="control-label" for="company">Company Name</label>
<div class="controls">
<input type="text" name="company">
<label for="company">Required !</label> // -> this line is generated by Plug-In.
<span class="help-inline">(required)</span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="username">User Name</label>
<div class="controls">
<input type="text" name="username">
<span class="help-inline">(required)</span>
</div>
</div>
Java scriptready(function() { $("#basic-form"). validate(); }); This is based on the assumption that you have already added the required JavaScript files. Adding those lines of JavaScript will make sure that your form is properly validated and shows all the error messages.
valid()Returns: Boolean Description: Checks whether the selected form is valid or whether all selected elements are valid.
You can only disable it after the form has successfully passed validation. Use the plugin's submitHandler option for this as it's fired on a button click only when the form has passed validation. $(). ready(function() {... is not recommended as per jQuery documentation.
It would be easier to leverage the built in options and let the plugin create your span
elements. The end result of the generated HTML will be what you're requesting.
Use errorElement
to change the <label>
into a <span>
.
Use errorClass
to change the default error class into help-inline
jQuery:
$(document).ready(function () {
$('#signUpForm').validate({ // initialize the plugin
errorElement: 'span',
errorClass: 'help-inline',
rules: {
company: "required",
username: "required"
},
messages: {
company: "Required !",
username: "Required !"
}
});
});
HTML:
<form id="signUpForm">
<div class="control-group">
<label class="control-label" for="inputCompanyName">Company Name</label>
<div class="controls">
<input name="company" type="text" id="inputCompanyName" placeholder="" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputFirst">User Name</label>
<div class="controls">
<input name="username" type="text" id="username" placeholder="" />
</div>
</div>
<input type="submit" />
</form>
DEMO: http://jsfiddle.net/eRZFp/
BTW: Your User Name
field was missing the name
attribute, name="username"
. For this plugin to work properly, all input elements must contain a unique name
attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With