jquery验证简单示例

来自《jquery 权威指南》

输入某个字符,选择相应的验证类型,并输出验证结果

-----------------------------------

效果显示:

详细代码:

<!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>jQuery</title>
<style type="text/css">
body,div,ul,li,p{margin: 0;padding: 0; font-size: 13px;}
ul{list-style-type: none;}
a{text-decoration: none;}
fieldset{width: 580px;}
fieldset div{padding: 8px;}
fieldset div select{font-size: 9pt;padding: 1px;}

#tip{margin-top: 10px;padding: 10px;border: solid 1px #666;background-color: #eee;width: 250px;display: none;}
.txt{border: solid 1px #666;padding: 2px;width: 120px;margin-right: 3px;}
.btn{border: solid 1px #666;padding: 2px;width: 60px;}

</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jqueryui/ui/jquery-ui.js"></script>
<script type="text/javascript">
;(function($){
    $.extend({
        chkStrByType: function(strS, chkType){
            var result;
            var chkStr = arrRegEx[chkType];
            var reg = RegExp(chkStr, 'g');
            result = reg.test(strS);
            return result;
        }
        
    });
    var arrRegEx = {};
    arrRegEx['email'] = '\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*';
    arrRegEx['telphone'] = '(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{7,8}';
    arrRegEx['postcode'] = '^\\d{6}$';
    arrRegEx['zh_cn'] = '[\\u4e00-\\u9fa5]';
    arrRegEx['number'] = '^-?[0-9]\\d*$';
    arrRegEx['url'] = '[a-zA-Z]+://[^\\s]*';
})(jQuery);

$(function(){
    $('#btn').click(function(){
        var strChk = $("#chkStr").val();
        var chkType = $("#selType option:selected").val();
        var res = $.chkStrByType(strChk,chkType);
        
        var strShow = "";
        var strType = res ? "" : "不是";

        strShow = "\"" + strChk + "\"" + strType + $("select :selected").text() + "类型";
        $("#tip").show().html("").append(strShow);
    });    
})

</script>
</head>
<body>

<fieldset>
    <legend>字符串类型检测</legend>
    <div>
        <span>检测内容:</span>
        <input id="chkStr" type="text" class="txt" />
        <span>选择类型:</span>
        <select id="selType">
            <option value="email">邮箱</option>
            <option value="telphone">电话号码</option>
            <option value="postcode">邮箱编码</option>
            <option value="number">整数</option>
            <option value="zh_cn">汉字</option>
            <option value="url">网址</option>
        </select>
        <input id="btn" type="button" value="检测" class="btn" />
        <div id="tip"></div>
    </div>
</fieldset>


</body>
</html>

 

转载于:https://www.cnblogs.com/phpway/p/3488310.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值