Javascript限制用户名字段中的特殊字符

时间:2014-06-23 10:25:09

标签: javascript

我在互联网上找到了一个预制表格,我正在整合到我的网站中。但是,此注册表允许使用任何字符。我试图将它限制为A-Z,a-z,0-9,下划线和空格。

$(function(){

var form    = $('form');
var submit  = $('#submit');
var alert   = $('.alert');



$.validator.addMethod("regex", function(value, element, regexp) {
var check = false;
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"No special Characters allowed here. Use only upper and lowercase letters (A through Z; a through z), numbers and punctuation marks (. , : ; ? ' ' \" - = ~ ! @ # $ % ^ & * ( ) _ + / < > { } )"
);



// validate form
form.validate({
    // validation rules
    rules: {
        // name field (required , minimum length 3)
        name: {
            required: true,
            minlength: 3,
            maxlength: 25
        },
        // username field (required , minimum length 3, max 8)
        username: {
            required: true,
            minlength: 3,
            maxlength: 8,
            regex: /^[a-zA-Z0-9_]+$/
        },
        // password field (required , minimum length 6, max 16)
        password: {
            required: true,
            minlength: 6,
            maxlength: 16
        },
        // password2 field must be equal to password field
        password2: {
            equalTo: '#password'
        },
        // email field only required
        email: 'required'
    },
    // submit ajax request
    submitHandler: ajaxSubmit
});

// jQuery ajax submit function
function ajaxSubmit() {
    $.ajax({
        url: 'ajax.php',
        type: 'POST',
        // form serialize data
        data: form.serialize(),
        beforeSend: function(){
            alert.fadeOut();
            submit.val('Sending...').attr('disabled', 'disabled');
        },
        success: function(data){
            console.log(data);
            if ( data.status === 'success' ) {
                // if responce status == success redirect to success page
                $(location).attr('href','success.html');
            } else {
                // not success! show error messages
                alert.html(data.status).fadeIn();
                submit.val('Sign Up').removeAttr('disabled');
            }
        },
        error: function(e){
            console.log(e);
            // show error message
            alert.html('Sending request fail').fadeIn();
            submit.val('Sign Up').removeAttr('disabled');
        }
    });
};
});

这是我正在尝试编辑的javascript代码。我只是不确定要为用户名添加什么来限制字符。我试过搜索谷歌,但我找到的所有答案都提出了一个功能,我不确定如何添加到这个。

1 个答案:

答案 0 :(得分:0)

检查此链接denying special characters jQuery Validate及以下代码

$.validator.addMethod(
"regex",
function(value, element, regexp) {
    var check = false;
    var re = new RegExp(regexp);
    return this.optional(element) || re.test(value);
},
"No special Characters allowed here. Use only upper and lowercase letters (A through Z; a through z), numbers and punctuation marks (. , : ; ? ' ' \" - = ~ ! @ # $ % ^ & * ( ) _ + / < > { } )"
);


username:{
required: true,
        minlength: 3,
        maxlength: 8,
         regex:/^[a-zA-Z0-9_]+$/
}