As no one seems to have had a crack at this, here goes!
First, download jQuery and jQuery.validate and include them in your theme
To populate your fields use this html markup (or something similar):
<label for="author">Author</label><input type="text" value="Your default text" id="author" name="author" />
and so on – you’ll note I’ve set a value, that’s what we’ll use for the next bit:
$(document).ready(function(){
$.fn.search = function() {
return this.focus(function() {
if( this.value == this.defaultValue ) {
this.value = "";
}
}).blur(function() {
if( !this.value.length ) {
this.value = this.defaultValue;
}
});
};
$("#author").search();
});
The above creates a function with jQuery to add/remove the default value on focus/blur.
You can then validate with the validation plugin, but you’ll need to add a couple of new methods to stop your default values being seen as valid entries. You do this like so:
$.validator.addMethod("author", function(value) {
return value != "Your default text";
}, 'Please enter your name');
When assigned to the input field, that will stop “Your default text” being returned as a ‘true’ value.
Hope this helps someone! 🙂
(@wordpressoring)
15 years, 6 months ago
Hi guys,
This is a last resort as I have tried coding away for a hours tonight to no avail.
I am trying to prevent default values assigned to my text fields such as,
..from being submitted when a user posts his or her comment.
I am using a Javascript implementation to clear the default value out of the text field when the user clicks into it (onfocus).
The Javascript I am using has all the neccessary functions to prevent default values from being submitted when the user posts the form having not filled out its required fields but I can not get it to work.
I end up with the error message:
Is there a simple way to go about this, to have my comments form validate itself based upon the rule that,
1) All required fields must be completed
&
2) Default values assigned to input fields can not be submitted
?
This was my code (though I think I am way off track)
JAVASCRIPT
HTML
And could someone kindly tell me what,
<?php echo $comment_author_email; ?>
..actually does and how it works?
Kind regards.