上传两张警告图片至web工程的根路劲的image文件下:
check_error.gif
check_right.gif
html代码:
<tr>
<td align = "right">
姓 名
<font color="red">*</font>
</td>
<td width="50%" align = "left">
<input name="userName" id="userName" type="text"
class="text" onBlur="checkUserName()"/> <span id="warmUserName"> </span>
</td>
</tr>
js代码:
//验证用户名
function checkUserName() {
var userName = $("input[name='userName']").val();
var cu = document.getElementById('warmUserName');
if(userName==""){
cu.style.display = "none";
return true;
}
var unLen = userName.replace(/[^\x00-\xff]/g, "**").length;
if(unLen < 3 || unLen > 15) {
warning(cu, unLen < 3 ? '用户名不能小于3个字符' : '用户名不能超过 15 个字符');
return false;
}
cu.style.display = '';
cu.innerHTML = '<img src="image/check_right.gif" width="13" height="13">';
return true;
}
function warning(obj, msg) {
obj.style.display = '';
obj.innerHTML = '<img src="image/check_error.gif" width="13" height="13"> ' + msg;
obj.className = "warning";
}
样式代码:
.warning{color:red;font-weight:bold;}