http://jsfiddle.net/ZKsjr/ 我正在使用这个脚本,它能够正确验证具有样式的输入字段,但我正在尝试实现一个新功能,以便它重置为原始值名称(名字)如果使用了不正确的信息(var reg = /^[az ,.'-]+$/i;),但这需要在他们完成输入后发生(模糊),你知道如何解决这个问题吗?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script type='text/javascript' src='//code.jquery.com/jquery-1.10.1.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
function isfname(text){
var reg = /^[a-z ,.'-]+$/i;
return reg.test(text);
}
$(".fname").keyup(function(e) {
var $test = $(this);
if (isfname($test.val())) {
$test.css("border-color", "rgb(140, 202, 165)");
$test.css("background-color", "rgb(140, 202, 165)");
$test.css("color", "black");
}else{
$test.css("background-color", "rgb(198, 95, 88)");
$test.css("border-color", "rgb(198, 95, 88)");
$test.css("color", "black");
}
}).blur(function(e) {
$(this).css("background-color", "");
$(this).css("border-color", "");
$(this).css("color", "");
});
});//]]>
</script>
</head>
<body>
<input type="text"id="element_2_2" class="fname" maxlength="255" size="8" " value="First Name"
onblur="if (this.value == '') {this.value = 'First Name';}"
onfocus="if (this.value == 'First Name') {this.value = '';}" required/>
</body>
</html>