zoukankan      html  css  js  c++  java
  • 常用的js效验

    <!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>emp.html</title>
    </head>


    <body>
    <form name="empForm" id="empForm" method="post" action="user.html">
        <table border="1"> 
            <tr>
            <td>真是姓名(不能为空,没有其他要求)</td>
                    <td><input type="text" id="realname" name="realname" /></td>
                </tr>
                <tr>
            <td>登录名(登录名不能为空,长度应该在5-8之前,可以包含中文字符(一个汉字算一个字符)):</td>
                    <td><input type="text" id="username" name="username" /></td>
                </tr>
                <tr>
            <td>密码(不能为空,长度在6-12字符或数字,不能包含中文字符)</td>
                    <td><input type="password" id="psw" name="psw" style="120px;" /></td>
                </tr>
                <tr>
            <td>重复密码(不能为空,长度在6-12字符或数字,不能包含中文字符)</td>
                    <td><input type="password" id="psw2" name="psw2" style="120px;" /></td>
                </tr>
                <tr>
            <td>性别(必选其一)</td>
                    <td>
                    <input type="radio" id="gender_male" value="m" name="gender" />男
                        <input type="radio" id="gender_female" value="f" name="gender" />女
                    </td>
                </tr>
                <tr>
            <td>身份证(15或18位)</td>
                    <td><input type="text" id="cart" name="cart" size="20" value="" /></td>
                </tr>
                <tr>
            <td align="right"><input type="button" name="ok" id="ok" value="保存" /></td>
                    <td></td>
                </tr>
        
                
            </table>
            
        </form>
        
        


    </body>
    <script language="javascript">
    window.onload=function(){
    document.getElementById("ok").onclick=function(){
    //alert("aa")
    //<td><input type="text" id="realname" name="realname" /></td>
    var realname=document.getElementById("realname").value;
    if(realname=='null'||realname==''){
    alert("你的真实姓名不能为空");
    document.getElementById("realname").focus();
    return false;
    }
    /*********************************************************************************************/
    //登录名 <input type="text" id="username" name="username" />
    var username=document.getElementById("username").value;
    if(username=='null'||username==''){
    alert("你的登录名不能为空");
    document.getElementById("username").focus();
    return false;
    }
    //验证长度不能小于5 匹配字母、数字、中文
    var pattern=new RegExp("^[A-Za-z0-9u4e00-u9fa5]{0,4}$");
    var flag=pattern.test(username);
    //alert(flag);
    if(flag){
    alert("你输入的登录名不能小于5个字符");
    document.getElementById("username").focus();
    return false;
    }

    //验证长度不能大于8 匹配字母、数字、中文 方法2用文本的形式写
    pattern=/^[A-Za-z0-9u4e00-u9fa5]{9,}$/;
    if(pattern.test(username)){
    alert("你输入的登录名不能大于8个字符");
    document.getElementById("username").focus();
    return false;
    }
    /*********************************************************************************************/
    //密码(不能为空,长度在6-12字符或数字,不能包含中文字符)
    //<input type="password" id="psw" name="psw" style="120px;" />
    var psw=document.getElementById("psw").value;
    if(psw=='null'||psw==''){
    alert("你的密码不能为空");
    document.getElementById("psw").focus();
    return false;
    }
    //验证密码长度不小于6个字符
    var pattern=/^[A-Za-z0-9u4e00-u9fa5]{0,5}$/;
    if(pattern.test(psw)){
    alert("您输入的密码长度不能小于6个字符");
    document.getElementById("psw").focus();
    return false;
    }
    //验证密码长度不能大于12个字符
    pattern=/^[A-Za-z0-9u4e00-u9fa5]{12,}$/;
    if(pattern.test(psw)){
    alert("您输入的密码长度不能大于12个字符");
    document.getElementById("psw").focus();
    return false;
    }
    /*********************************************************************************************/
    //<input type="password" id="psw2" name="psw2" style="120px;" />
    var psw2=document.getElementById("psw2").value;
    if(psw!=psw2){
    alert("两次密码不一致")
    document.getElementById("psw2").focus();
    return false;
    }
    /*********************************************************************************************/
    //性别(必选其一)
    //<input type="radio" id="gender_male" value="m" name="gender" />男
                    //<input type="radio" id="gender_female" value="f" name="gender" />女
    var flag=false;
    var genderElements=document.getElementsByName("gender");
    for(var i=0;i<genderElements.length;i++){
    if(genderElements[i].checked){
    flag=true;
    break;
    }
    if(!flag){
    alert("您没有第三种性别");
    return false;
    }
    }
    /*********************************************************************************************/
    //身份证效验
    //<td>身份证(15或18位)</td>
                    //<td><input type="text" id="cart" name="cart" size="20" value="" /></td>
    var cart=document.getElementById("cart").value;
    if(cart=='null'||cart==''){
    alert("您的身份证不能为空");
    document.getElementById("cart").focus();
    return false;
    }
    var len=cart.length;
    if(len!=15&&len!=18){
    alert("您输入的身份证有误");
    document.getElementById("cart").focus();
    return false;
    }
    //验证身份证是15位
    alert("len="+len);
    if(len == 15){
    var pattern=/^d{15}$/;
    if(!pattern.test(cart)){
    alert("15位身份证输入有误");
    document.getElementById("cart").focus();
    return false;
    }
    }
    //验证身份证是18位
    //alert("len="+len);
    if(len == 18){
    var pattern=/^d{18}$/;
    if(!pattern.test(cart)){
    alert("18位身份证输入有误");
    document.getElementById("cart").focus();
    return false;
    }
    }

    }
    }
    </script>




    </html>

  • 相关阅读:
    JS Table排序类
    JavaScript使用技巧精萃
    修改鄒建 老師的SQL PivotTable,增加同分組非交叉欄位
    类似gmail添加附件
    [转贴]Js中 关于top、clientTop、scrollTop、offsetTop等
    Three Tier Code generation with Codesmith
    SQL中取得漢字拼音首字母或五筆首鍵編碼
    (转)ComputerStyle与currentStyle的区别
    html css样式色彩解析
    js 拖拽效果
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3230751.html
Copyright © 2011-2022 走看看