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

表单字符匹配的问题
<script   language= "javascript ">
function   regcheck(formct)   {
if   (formct.id.value   !=[a-g])   {
alert( '编码非法 ');
formct.id.focus();  
return   false;
}
</script>

<body>
<form   action= "in.php "   method= "post "   name= "form1 "   target= "_blank "   onSubmit= 'return   regcheck(this) '>
<input   name= "id "   type= "text "   id= "id "   size= "10 "   maxlength= "10 ">
</form>

问题为表单id只可输入a-g之间的字母(可输入a,也可输入abcdefg),除外都为非法

------解决方案--------------------
if ( !/^[a-g]+$/.test(formct.id.value) ) {
------解决方案--------------------
<!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=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function regcheck(){
var reg=/^[a-g]+$/i;
var text=document.getElementById("test");
if(!reg.test(text.value)){
alert( '编码非法 ');
text.focus();
return false;
}
return true;
}
</script>

<body>
<form action= "in.php " method= "post " name= "form1 " target= "_blank " onsubmit="return regcheck()">
<input type= "text" id="test" size= "10 " maxlength= "10 ">
</form> 
</body>
</html>
这样?