javascirpt验证

来源:互联网 发布:java开发平台下载 编辑:程序博客网 时间:2024/05/09 02:45

 document.location.href='/wer.so' //提交请求
script:history.back()            //返回

应用
一 基本表单验证
<form  name="form1" onsubmit="return checkForm(form1)">
            用户名:<input type="text" id="id" name="name2" /><br/>
            F<input type="radio" name="radioq" value="1"/>
            M<input type="radio" name="radioq" value="2"/><br/>

            A<input type="checkbox" name="checkbox" value="q"/>
            B<input type="checkbox" name="checkbox" value="wq"/>
            C<input type="checkbox" name="checkbox" value="wq"/><br />

            <select name="select">
                <option value="0">请选择</option>
                <option value="1">选择1</option>
                <option value="2">选择2</option>
            </select><br/>
            <input type="submit" value="teus"/>
</form>

<script>
            function checkForm(form){
                var flg = true;
                if(form.name2.value.length <= 0){
                    flg = false;
                    alert("name is null");
                }
                else if(!form.radioq[0].checked && !form.radioq[1].checked){
                    flg = false;
                    alert("gender is null");
                }else if(!checkCheckBox(form.checkbox)){
                    flg = false;
                    alert("c is null");
                }else if(form.select.value == "0"){
                    flg = false;
                    alert("s is null");
                }
                return flg;
            }
            function checkCheckBox(checkbox){  //此方法为验证多选框即checkbox另外写的!
                var flg = false;
                for(i=0;i<checkbox.length;i++){
                    if(checkbox[i].checked){
                        flg = true;
                    }
                }
                return flg;
            }
</script>

二 其他特殊验证

 

原创粉丝点击