日期:2014-05-16  浏览次数:20361 次

各位大神关于javascript和web方面的问题
我想实现一个这样的功能:在jsp中弄一个文本框,输入字符进去的时候,用正则表达式去判断里面的字符长度是否超过范围,超过了就在文本旁边提醒字符长度允许的范围,如果没有超过,提示正确即可。
------解决方案--------------------
获得textarea 节点后.value.length获取长度试试 

------解决方案--------------------
关键是用户的输入内容要满足怎样的条件,其他的都是简单的判断
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.test{width:400px;height:30px;margin:100px auto;}
input{padding:5px 0 5px 5px;float:left; width:198px;background:none;border:solid 1px #ccc;height:20px;}
.test span{float:left;font-size:12px;height:30px;line-height:30px; padding-left:10px;}
.awokeW{color:red;}
.awokeR{color:green;}
</style>
<script>
window.onload=function()
{
var oInput=document.getElementById('username');
var oSpan=document.getElementById('awoke');
oInput.onkeyup=function()
{
if(/^\w{6,10}$/g.test(this.value))
{
oSpan.className='awokeR';
oSpan.innerHTML='填写正确';
}
else
{
oSpan.className='awokeW';
oSpan.innerHTML='请确保填写内容在6-10个字符';
}
}
}
</script>
</head>

<body>
<div class="test">
<input type="text" id="username" name="username" />
    <span class="awokeW" id="awoke"></span>
</div>
</body>
</html>

------解决方案--------------------
var re=/[\u4e00-\u9fa5]/g;
alert(!re.test('中文') ? 'Y' : 'N');
alert(!re.test('中文2x') ? 'Y' : 'N');