Hi freefall,
This is tricky. You can't use the standard Validation code alone because that just checks for a value and your inputs always have the label as a value.
Here's the code for a form that I set up like this:
[attachment=0]05-06-2010 14-37-12.png[/attachment]
You can see that there are labels in the inputs and changed error messages.
The HTML code is fairly standard excpt that there are exta spans for the error messages and a little snippet at the end to load the JavaScript file:
<div class="form_item">
<div class="form_element cf_textbox">
<label class="cf_label" style="width: 150px;">name</label>
<input class="cf_inputbox required" maxlength="150" size="30" title="A name is required" id="text_0" name="name" type="text" />
<span class='LV_validation_message' />Â </span>
</div>
<div class="cfclear">Â </div>
</div>
<div class="form_item">
<div class="form_element cf_textbox">
<label class="cf_label" style="width: 150px;">company</label>
<input class="cf_inputbox" maxlength="150" size="30" title="" id="text_1" name="company" type="text" />
<span class='LV_validation_message' />Â </span>
</div>
<div class="cfclear">Â </div>
</div>
<div class="form_item">
<div class="form_element cf_textbox">
<label class="cf_label" style="width: 150px;">email</label>
<input class="cf_inputbox required validate-email" maxlength="150" size="30" title="A valid email is required" id="text_2" name="email" type="text" />
<span class='LV_validation_message' />Â </span>
</div>
<div class="cfclear">Â </div>
</div>
<div class="form_item">
<div class="form_element cf_textbox">
<label class="cf_label" style="width: 150px;">telephone</label>
<input class="cf_inputbox" maxlength="150" size="30" title="" id="text_3" name="telephone" type="text" />
<span class='LV_validation_message' />Â </span>
</div>
<div class="cfclear">Â </div>
</div>
<div class="form_item">
<div class="form_element cf_textarea">
<label class="cf_label" style="width: 150px;">information requested</label>
<textarea class="cf_inputbox" rows="3" id="text_4" title="" cols="30" name="request"></textarea>
<span class='LV_validation_message' />Â </span>
</div>
<div class="cfclear">Â </div>
</div>
<div class="form_item">
<div class="form_element cf_button">
<input value="Send" name="submit" id='submit' type="submit" />
</div>
<div class="cfclear">Â </div>
</div>
<?php
if ( !$mainframe->isSite() ) { return; }
$doc =& JFactory::getDocument();
$doc->addScript(JURI::base().'components/com_chronocontact/includes/contact/contact.js');
?>
Here is the JavaScript. This is quite long but is actually mostly repetitive:
window.addEvent('domready', function() {
$('ChronoContact_contact').getElements('input[type=text]').each(function(my_input) {
var label = my_input.getParent().getElement('label');
if ( !my_input.value ) {
my_input.value = label.get('text');
my_input.setStyle('color', '#888');
label.setStyle('display', 'none');
}
showLabel(my_input);
});
$('ChronoContact_contact').getElements('textarea' ).each(function(my_input) {
var label = my_input.getParent().getElement('label');
if ( !my_input.value ) {
my_input.value = label.get('text');
my_input.setStyle('color', '#888');
label.setStyle('display', 'none');
}
showLabel(my_input);
});
$('submit').addEvent('click', function() {
$('ChronoContact_contact').getElements('input[type=text]').each(function(my_input) {
var label = my_input.getParent().getElement('label');
if ( my_input.value == label.get('text') ) {
my_input.value = '';
my_input.setStyle('color', '#000');
}
});
$('ChronoContact_contact').getElements('textarea' ).each(function(my_input) {
var label = my_input.getParent().getElement('label');
if ( my_input.value == label.get('text') ) {
my_input.value = '';
my_input.setStyle('color', '#000');
}
});
});
$('submit').addEvent('click', function() {
$('ChronoContact_contact').getElements('input[type=text]').each(function(my_input) {
var label = my_input.getParent().getElement('label');
if ( my_input.value == label.get('text') ) {
my_input.value = '';
my_input.setStyle('color', '#000');
}
});
$('ChronoContact_contact').getElements('textarea' ).each(function(my_input) {
var label = my_input.getParent().getElement('label');
if ( my_input.value == label.get('text') ) {
my_input.value = '';
my_input.setStyle('color', '#000');
}
});
});
$('submit').addEvent('blur', function() {
$('ChronoContact_contact').getElements('input[type=text]').each(function(my_input) {
var label = my_input.getParent().getElement('label');
if ( my_input.value == '' ) {
my_input.value = label.get('text');
my_input.setStyle('color', '#888');
}
});
$('ChronoContact_contact').getElements('textarea' ).each(function(my_input) {
var label = my_input.getParent().getElement('label');
if ( my_input.value == '' ) {
my_input.value = label.get('text');
my_input.setStyle('color', '#888');
}
});
});
});
function showLabel(my_input) {
//var my_input = $('text_0');
var label = my_input.getParent().getElement('label');
if ( !my_input.value ) {
my_input.value = label.get('text');
label.setStyle('display', 'none');
}
my_input.addEvent('focus', function() {
var label = this.getParent().getElement('label');
if ( this.value == label.get('text') ) {
this.value = '';
this.setStyle('color', '#000');
}
});
my_input.addEvent('blur', function() {
var label = this.getParent().getElement('label');
if ( this.value == '' ) {
this.value = label.get('text');
this.setStyle('color', '#888');
}
});
my_input.addEvent('submit', function(){
console.log('onsubmit');
var label = this.getParent().getElement('label');
if ( this.value == label.get('text') ) {
this.value = '';
this.setStyle('color', '#000');
}
});
};
First all the key blocks are repeated, once for the text inputs and again for the textarea (this could probably be improved with better coding).
The first pair of blocks copies the input label into the input and hides the label; this also sets 'click' and 'blur' events that will remove the label when you click into the input and put it back if the input is left empty. (This uses the showLabel() function at the end.)
The second and this pair removes the label when you click on the submit button so that the normal LiveValidation will work OK; and replaces the labels if the form doesn't submit because there is a validation error.
Bob