简单的jQuery表单正则验证和提交

来源:互联网 发布:人民警察证制作软件 编辑:程序博客网 时间:2024/06/16 10:54

HTML结构如下:

<form id="yuyueform" class="yuyueform" target="_blank" method="post" action=""><div class="input_wrap input_name">    <input type="text" id="name" value="" name="name" class="input_txt"  placeholder="姓名" >    </div><div class="input_wrap input_phonenum">    <input type="text" id="phonenum" value="" name="phonenum" class="input_txt"  placeholder="联系方式" >    </div>    <div class="button_content"><a id="OpenUser" href="javascript:;">立即提交</a></div></form>

首先是先把正则写好,可以这样:

            // 验证中文名称            function isChinaName(name) {             var pattern = /^[\u4E00-\u9FA5]{1,6}$/;             return pattern.test(name);            }            // 验证手机号            function isPhoneNo(phone) {              var pattern = /^1[34578]\d{9}$/;              return pattern.test(phone);             }function SubmitMsg(){    var name = $("#name").val();    var phonenum = $("#phonenum").val();    if(name.length == 0){          layer.alert('请输入你的用户名');          $("#name").focus();          return false;    }    if(isChinaName(name) == false){        layer.alert('用户名不合法');         $("#name").focus();          return false;    }    if(phonenum.length == 0){            layer.alert('请输入你的手机号');            $("#phonenum").focus();          return false;     }    if(isPhoneNo(phonenum) == false){        layer.alert('手机号格式不正确');            $("#phonenum").focus();          return false;             }    $.ajax({                 type : 'post',                 dataType : 'json',                 url : '提交地址....',                 data:{name:name,phonenum:phonenum},                 success:function(data){                      if(data.status == 2){                         layer.alert('提交成功');                           $("#name").val('');                          $("#phonenum").val('');                        }else{                        layer.alert('你是怎么找到我的 - -');                      }                 }     });    }    

最后绑定事件:

$(document).ready(function(){    $("#OpenUser").bind('click',SubmitMsg);});
0 0
原创粉丝点击