jQuery实现上传附件为空和文件格式校验

HTML代码片段

<th>正式文本:</th>
<td>
    <input type="file" name="f_zswb" id="f_zswb" class="djinput" onblur="zswbjy();" />
    <div id="zswbdiv"></div>
</td>

JS校验方法的实现代码

<script type="text/javascript">
//正式文本校验
function zswbjy(){
    var zswb = $("#f_zswb").val();
    if(zswb.length>1 && zswb != ''){
        var lodt = zswb.lastIndexOf(".");
        var type = zswb.substring(lodt+1);
        if(type != "doc" && type != "docx"){
            $("#zswbdiv").empty();
            $("#zswbdiv").append("<font color='red'>上传的文件必须为doc或者docx格式!</font>");
            return false;
        }else{
            $("#zswbdiv").empty();
            return true;
        }
    }else{
        $("#zswbdiv").empty();
        $("#zswbdiv").append("<font color='red'>正式文本不能为空!</font>");
        return false;
    }
}
</script>
posted @ 2015-04-07 09:48  夜月光雾  阅读(21)  评论(0编辑  收藏  举报