jquery验证文本框,以及特殊字符过滤

//用户名写入"请输入管理员用户名!"js
    $("#txtName").val("请输入管理员用户名!");
    //用户名写入"请输入管理员用户名!"js
    //用户名输入js
    //当文本框等到焦点
    $("#txtName").focus(function () {
        if ($("#txtName").val() == "请输入管理员用户名!") {
            $("#txtName").val("");
        }
    });
    //当文本框失去焦点
    $("#txtName").blur(function () {
        var checking = false;
        var txtName = $("#txtName").val();
        //当文本框为空
        if (txtName == "") {
            $("#txtName").val("请输入管理员用户名!");
            $("#prompt_txtName").text("请输入管理员用户名!");
            $("#prompt_txtName").text("用户名不能为空!");
        }
        //当文本框没有变化
        else if (txtName == "请输入管理员用户名!") {
            $("#prompt_txtName").text("请按要求填写用户名!");
        }
        //只允许汉字、英文字母、数字、下划线!
        else if (!txtName.match(/^[\u4E00-\u9FA5a-zA-Z0-9_]{0,}$/)) {//.match(/^[\u4E00-\u9FA5a-zA-Z0-9_]{3,20}$/) //{3,20}$表示是长度3-20
            $("#prompt_txtName").text("只允许汉字、英文字母、数字、下划线!");
        }
        //邮箱验证
        else if (!txtName.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)) {
            $("#prompt_txtName").text("请输入正确的邮箱格式!");
        }
        //文本框的内容的长度
        else if (txtName.length > 20 || txtName.length < 3) {
            $("#prompt_txtName").text("长度在3-20之间!");
        }
        else {
        //判断 用户是否存在
            $.ajax({
                type: "post",
                dataType: "Text",
                url: "Login.aspx",
                data: { judge: txtName },
                //beforeSend: function () {//未发送执行的事件
                //alert("dd");;
                //},
                success: function (data) {//data就是返回的那个字符串
                    if (data == "Exist") {
                        checking = true;
                        $("#prompt_txtName").text("");
                    }
                    else {
                        $("#prompt_txtName").text("此用户不存在!");
                    }
                },
                error: function (err) {
                    alert("请刷新后重试!");
                }
            });
        }
        if (checking) {
            $("#imgName").attr("src", "SystemImage/false.png");
            $("#imgName").show();
        }
        else {
            $("#imgName").attr("src", "SystemImage/true.png");
            $("#imgName").show();
        }
    });
    //用户名输入js
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值