JavaScript验证长度
Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<script language="javascript">
var const_device_name_len = 32;
var const_time_server_max_len = 128;
function ConstSyslen()
{
var error_message1 = "";
var error_message2 = "";
device_name_len = device_name.value.length
ntp_server_len = ntp_server.value.length
var sysconfig_errortips = document.getElementById("sysconfig_errortips");
var item_device_name = document.getElementById("item_device_name");
var item_ntp_server = document.getElementById("item_ntp_server");
var bl = false;
if (device_name_len>const_device_name_len)
{
error_message1 = "<br>"+item_device_name.innerHTML+"不能超过"+const_device_name_len+"个字符";
bl = true;
}
if (ntp_server_len>const_time_server_max_len)
{
error_message2 = "<br>"+item_ntp_server.innerHTML+"不能超过"+const_time_server_max_len+"个字符"+"<br>";
bl = true;
}
if(bl)
{
sysconfig_errortips.innerHTML = "保存出错"+error_message1+error_message2;
return false;
}
}
</script>
</head>
<body>
<div id = "item_device_name">设备名称</div>
<div>
<input type = "text" name="device_name" />
</div>
<div id = "item_ntp_server">设备名称</div>
<div>
<input type = "text" name="ntp_server" />
</div>
<div id = "sysconfig_errortips" style =" color:Red;"></div>
<div>
<input type = "button" value = "保存" id = "btn_save" onclick = "ConstSyslen()"/>
</div>
</body>
</html>