Her er et hurtigt smedet plugin til
www.jquery.com<style type="text/css">
.notfilled{background-color: Gray;}
.ok{background-color: White;}
.err{background-color:Red;}
</style>
<input type="text" id="Dato" name="Dato" maxlength="10"/>
<script type="text/javascript">
(function($){
$.fn.fieldFix = function(options) {
var defaults = {
defaultValue: "",
regEx: "",
emptyClass: "notfilled",
okClass: "ok",
errorClass: "err",
errorMessage: ""
};
var options = $.extend(defaults, options);
return this.each(function() {
curVal = $(this).val();
//Når siden hentes
if(curVal == '' || curVal == options.defaultValue)
{
$(this).val(options.defaultValue);
$(this).attr('class', options.emptyClass);
}
//Når brugeren sætter musen i feltet
$(this).focus(function(){
if($(this).val() == options.defaultValue)
{
this.value = "";
}
});
//Når brugeren forlader feltet
$(this).blur(function(){
if(this.value.match(options.regEx) != null)
{
$(this).attr("class", options.okClass);
$("#err_"+$(this).attr('id')).remove();
}else
{
$(this).attr("class", options.errorClass);
$(this).focus();
$(this).parent().append("<strong id='err_"+$(this).attr('id')+"'>"+options.errorMessage+"</strong>")
}
});
});
};
})(jQuery);
jQuery("#Dato").fieldFix({defaultValue:"dd-mm-åååå", regEx: /^\d{2}\-\d{2}\-\d{4}$/, errorMessage: "Skal være i formatet dd-mm-åååå"});
</script>