接口添加页面JS验证

接口添加页面JS验证
<script>
        //验证接口信息
 
        //验证接口是否是汉字
        function checkname(name){
                var preg=/[^\u4e00-\u9fa5]/g  
                if(name.value == ''){
                    document.getElementById('name').innerHTML="<font color='red'>*请填写接口中文名称</font>";
                }else if(name.value.match(preg)){
                    document.getElementById('name').innerHTML="<font color='red'>*接口中文名称必须是中文</font>";
                    return false;
                }else{
                    document.getElementById('name').innerHTML="<font color='green'>*填写正确</font>";
                    return true;
                }
        }
        //验证接口是否是汉字
        function checken_name(name){
            var preg=/^[a-zA-Z0-9_]+$/;  
            if(name.value == ''){
                document.getElementById('en_name').innerHTML="<font color='red'>*请填写接口英文名称</font>";
                return false;
            }else if(!preg.exec(name.value)){
                document.getElementById('en_name').innerHTML="<font color='red'>*接口英文名称必须是英文</font>";
                return false;
            }else{
                document.getElementById('en_name').innerHTML="<font color='green'>*填写正确</font>";
                return true;
            }
        }
        //验证单选框是否选中
        function checkradio(radio){
            //alert(radio.length); 
                for(var i=0;i<radio.length;i++){
                   if(radio[i].checked){
                       return true;
                       break;
                     }
                }
             alert('请选择接口类型');
             return false;
        }
 
           //下拉菜单是否选择。
        function checkgameid(value){
            //alert(a.gameid.value);
            for(var i=0;i<value.length;i++){
                if(value.value!=0){
                    return true;
                    break;
                }
            }
            alert('请选择业务')
            return false;
        }
 
        //判断所以的是否通过才能提交
        function docheck(form){
                var a=document.forms[0];
                if(checkname(a.interface_name) && checken_name(a.interface_en_name) && checkradio(a.ckbox) && checkgameid(a.gameid)){
 
                    return true;
            }else{ 
                return false;
            }
        }
        //验证是否要输入值。输入则请默认值清空
        function doinput(it){
            if(it.value=="接口英文名称" || it.value=="接口中文名称"){
                it.value="";
            }
        }
 
    </script>
 
 
 
 
 
 
 
 
 
<form action="add.php" method="post" onsubmit="return docheck(this)">
 
<table height="400" align="center">
<caption><h3><b>添加接口</b><h3></caption>
<tr>
<td align="right">
<font face="">业务:</font>
</td>
<td>
<select name='gameid'>
<option value='0'>请选择业务</option>
<!--输出业务ID及业务名称-->
<?php foreach ($gameid as $v):?>
<option value="<?php echo $v['id']?>"><?php echo $v['name']?></option>
<?php endforeach;?>
<!--输出业务ID及业务名称-->
</select>
</td>
</tr>
<tr>
<td align="right">
<font face="">请选择接口类型:</font>
</td>
<td>
<input type="radio" name="ckbox" value="1">查询
<input type="radio" name="ckbox" value="0">充值
<input type="radio" name="ckbox" value="2">更新
<input type="radio" name="ckbox" value="3">修改
<input type="radio" name="ckbox" value="4">删除
</td>
</tr>
<tr>
<td align="right">
<font face="">接口中文名称:</font>
</td>
<td>
<input type="text" onfocus='doinput(this)' value='接口中文名称' style="color:#666666" name="interface_name" onblur ='checkname(this)'><span id='name'><font color="red">*</font></span>
</td>
</tr>
<tr>
<td align="right">
<font face="">接口英文名称:</font>
</td>
<td>
<input type="text" onfocus='doinput(this)' value='接口英文名称' style="color:#666666" name="interface_en_name" onblur ='checken_name(this)'><span id='en_name'><font color="red">*</font></span>
</td>
</tr>
<tr>
<td colspan="2">
<input type="reset" size="8" value="重填">
<input type="submit" size="24" value="提交">
</td>
</tr>
 
</table>
</form>





posted @ 2012-05-25 15:26  holyes  阅读(361)  评论(0编辑  收藏  举报