- Your last custom message was missing the opening quotation mark,
"
.
- Your custom
messages
should contain parameter placeholders, {0}
, that are automatically replaced by the value of your rule.
- You also misspelled the
minlength
rule within the custom messages
option by using a capital L
.
Try this code instead:
$("form").validate({
rules:{
myName:{
required: true,
minlength: 2
},
myEmail:{
required: true,
email: true
},
myMessage:{
required: true,
minlength: 10
}
},
messages:{
myName:{
required: "Type your name",
minlength: "Your name must be at least {0} characters long"
},
myEmail:{
required: "Type your e-mail address",
email: "Type a valid e-mail address"
},
myMessage:{
required: "Type your message",
minlength: "Your message must be at least {0} characters long"
}
}
});
Demo: http://jsfiddle.net/3JTNh/
Quote OP: "I am wondering how I could replace custom jQuery Validation Plugin DOM messages with default browser alerts"
Your question is very unclear, but the way to dynamically change your messages would be to dynamically change your rules by using the rules('add')
method, and only specify the message(s) to change... Working Demo: http://jsfiddle.net/vkF9r/
* removed *
EDIT:
As per comments, the OP was originally asking for a JavaScript alert()
which can be done using the errorPlacement
callback function like this. Since error
is an object, you use error.text()
to only use its message.
onkeyup: false,
errorPlacement: function (error, element) {
alert(error.text());
},
I recommend setting onkeyup
to false
in order to negate a repeated alert on every keystroke. If you are using Safari, do not try this demo or you'll get stuck in an infinite loop (one shortcoming of using alert()
in this case) http://jsfiddle.net/kxwYd/
BTW: I do not recommend ever using the antiquated JavaScript alert()
within any website design.
Better/Recommended:
For a more modern user experience, you should instead integrate this with a jQuery modal or tooltip plugin like Tooltipster.
Working Example: http://jsfiddle.net/SfanE/
The following code is from my other answer here...
First, initialize the Tooltipster plugin (with any options) on all specific form
elements that will display errors:
$(document).ready(function () {
// initialize tooltipster on form input elements
$('#myform input[type="text"]').tooltipster({
trigger: 'custom', // default is 'hover' which is no good here
onlyOne: false, // allow multiple tips to be open at a time
position: 'right' // display the tips to the right of the element
});
});
Second, use Tooltipster's advanced options along with the success:
and errorPlacement:
callback functions built into the Validate plugin to automatically show and hide the tooltips as follows:
$(document).ready(function () {
// initialize validate plugin on the form
$('#myform').validate({
// any other options & rules,
errorPlacement: function (error, element) {
$(element).data('tooltipsterContent', $(error).text());
$(element).data('plugin_tooltipster').showTooltip();
},
success: function (label, element) {
$(element).data('plugin_tooltipster').hideTooltip();
}
});
});