PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > Jquery教程 > 正文

    使用Jquery验证注册表单数据代码分享

    作者:admin来源:iteye浏览:时间:2020-09-30 00:07:50我要评论
    导读:使用Jquery验证注册表单数据代码分享
     
    复制代码代码如下:
    1. /**
    2.  * @author jilongliang
    3.  * 注册的验证.
    4.  */
    5. $(function(){
    6.     var $email =/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/ // Email
    7.     var $userName =/^[\u4e00-\u9fa5_a-zA-Z0-9]+$/ // 匹配中文,英文字母和数字及
    8.     var $qq =/[1-9][0-9]{4,}/   // QQ ^[1-9]{1}[\d]{4,9}$
    9.     var $url =/^https?:\/\/(\w+\.)?[\w\-\.]+(\.\w+)+$/ // url
    10.     var $mycode =/^[0-9]*$/   // code
    11.     var img_rigth ="< img src='images/check_right.gif'/>";
    12.     var img_error ="< img src='images/check_error.gif'/>";
    13.  
    14.     /**
    15.      * 取得一个包含匹配的元素集合中每一个元素紧邻的后面同辈元素的元素集合.
    16.      * 这个函数只返回后面那个紧邻的同辈元素,而不是后面所有的同辈元素(可以使用nextAll) 可以用一个可选的表达式进行筛选.
    17.      */
    18.     $("input[name='username']").blur(function() {
    19.         if ($(this).val() == "") {
    20.            $(this).nextAll("span").html(img_error + "用户名不能为空!");
    21.            return;
    22.            // $('#email').focus;
    23.            // return false;
    24.        } else if ($userName.test($(this).val()) == false) {
    25.            $(this).nextAll("span").html(img_error + "格式不正确!");
    26.            return;
    27.        } else {
    28.            $(this).next("span").html(img_rigth);
    29.        }
    30.     });
    31.  
    32.     /** 判断密码 */
    33.     $("input[name='password']").blur(function() {
    34.        if ($(this).val() == "") {
    35.            $(this).nextAll("span").html(img_error + "密码不能为空!");
    36.            return;
    37.        } else {
    38.            $(this).next("span").html(img_rigth);
    39.        }
    40.     });
    41.  
    42.     /** 判断密码是否一致 */
    43.     $("input[name='comfirm_password']").blur(function() {
    44.        var password = $("input[name='password']").val();
    45.        if (password != $(this).val()) {
    46.            $(this).next("span").html(img_error+ "确认密码与密码一致.");
    47.            return;
    48.        } else {
    49.            $(this).next("span").html(img_rigth);
    50.        }
    51.     });
    52.  
    53.     /** 密码提示验证 */
    54.     $("input[name='pwd_prompt']").blur(function() {
    55.  
    56.        if ($(this).val()=="") {
    57.            $(this).next("span").html(img_error+"不能为空必须填写项.");
    58.            return;
    59.        } else if ($(this).val().length < 2) {
    60.            $(this).next("span").html(img_error + "长度不能小于两个字节.");
    61.            return;
    62.        } else {
    63.            $(this).next("span").html(img_rigth);
    64.        }
    65.     });
    66.  
    67.     /** 密码回答验证 */
    68.     $("input[name='pwd_answer']").blur(function() {
    69.        if ($(this).val()=="") {
    70.            $(this).next("span").html(img_error + "不能为空必须填写项.");
    71.            return;
    72.        } else if ($(this).val().length < 2) {
    73.            $(this).next("span").html(img_error + "长度不能小于两个字节.");
    74.            return;
    75.        } else {
    76.            $(this).next("span").html(img_rigth);
    77.        }
    78.     });
    79.  
    80.     /** Email验证 */
    81.     $("input[name='email']").blur(function() {
    82.        if ($(this).val()=="") {
    83.            $(this).next("span").html(img_error + "Email不能为空.");
    84.            return;
    85.        } else if ($email.test($(this).val()) == false) {
    86.            $(this).next("span").html(img_error + "您的Email格式不正确!");
    87.            return;
    88.        } else {
    89.            $(this).next("span").html(img_rigth);
    90.        }
    91.     });
    92.     /** 验证QQ */
    93.     $("input[name='qq']").blur(function() {
    94.        if ($(this).val()=="") {
    95.            return null;
    96.        } else if ($qq.test($(this).val()) == false) {
    97.            $(this).next("span").html(img_error + "您的QQ格式不正确!");
    98.            return;
    99.        } else {
    100.            $(this).next("span").html(img_rigth);
    101.        }
    102.     });
    103.     /** 网址验证 */
    104.     $("input[name='url']").blur(function() {
    105.        if ($(this).val()=="") {
    106.            return null;
    107.        } else if ($url.test($(this).val()) == false) {
    108.            $(this).next("span").html(img_error + "您的URL格式不正确!");
    109.            return;
    110.        } else {
    111.            $(this).next("span").html(img_rigth);
    112.        }
    113.     });
    114.     /** 验证码 */
    115.     $("input[name='code']").blur(function() {
    116.        if ($(this).val()=="") {
    117.            $("#img_code").html(img_error + "验证码不能为空!");
    118.            return;
    119.        } else if ($mycode.test($(this).val()) == false) {
    120.            $("#img_code").html(img_error + "验证码格式不正确!");
    121.            return ;
    122.        }else{
    123.            $("#img_code").html(img_rigth);
    124.        }
    125.     });
    126.     /**
    127.      * 提交
    128.      */
    129.     $("#reg_submit").click(function(){
    130. // if ($("#reg_username").val() == "") {
    131. // $("#sp_name").nextAll("span").html(img_error + "用户名不能为空!");
    132. // return;
    133. // }else if($userName.test($("#reg_username").val()) == false) {
    134. // $("#sp_name").nextAll("span").html(img_error + "格式不正确!");
    135. // return;
    136. // }else if($("#reg_code").val()==""){
    137. // $("#img_code").nextAll("span").html(img_error + "验证码不能为空!");
    138. // return;
    139. // }else if($mycode.test($("#reg_code").val()) == false){
    140. // $("#img_code").nextAll("span").html(img_error + "格式不正确!");
    141. // return;
    142. // } else {
    143. // $("#img_code,#sp_name").html(img_rigth);
    144. // }
    145.            var $username = $("#reg_username").val();// 用户名
    146.            var $password = $("#reg_password").val();// 密码
    147.            var $comfirm_password = $("#reg_comfirm_password").val();// 确认密码
    148.            var $pwd_prompt = $("#reg_pwd_prompt").val();// 密码提示
    149.            var $pwd_answer = $("#reg_pwd_answer").val();// 密码问题
    150.            // var $sex=$("#sex").val();//性别
    151.            var $face = $("#reg_face").val();// 用户 头像
    152.            var $email = $("#reg_email").val();// Email
    153.            var $qq = $("#reg_qq").val();// qq
    154.            var $url = $("#reg_url").val();// 主页URL
    155.            var $code = $("#reg_code").val();// 验证码
    156.            $.post('register.action',{
    157.                type:'post',
    158.               dataType:'json',
    159.               username : $username,
    160.               password : $password,
    161.               comfirm_password : $comfirm_password,
    162.               pwd_prompt : $pwd_prompt,
    163.               pwd_answer : $pwd_answer,
    164.               face : $face,
    165.               email : $email,
    166.               qq : $qq,
    167.               myurl : $url,
    168.               code : $code
    169.            }, function(data) {
    170.               //data.flag是访问struts2提供的set与get.
    171.               if(data.flag==true){
    172.                   location.href="index.jsp";
    173.                   //event.preventDefault()//防止默认行为(表单提交)
    174.               }else{
    175.                    alert("注册失败啦!");
    176.               }
    177.            });
    178.     });
    179.  
    180. });
    181.  
    182. /** 验证码的函数是Javascript,是在Jquery function之外 */
    183. function ValidCode(obj) {
    184.     var timenow = new Date().getTime();
    185.     obj.src = "rand.action?d=" + timenow;
    186. }

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-47-312-1.html
    相关热词搜索: 表单验证 Jquery教程