开发者

jQuery regex, remote with validation

开发者 https://www.devze.com 2023-01-17 03:14 出处:网络
I have this: <script type=\"text/javascript\"> jQuery().ready(function() { // validate signup form on keyu开发者_如何学JAVAp and submit

I have this:

    <script type="text/javascript">

jQuery().ready(function() {

    // validate signup form on keyu开发者_如何学JAVAp and submit
    jQuery("#regform").validate({
        rules: {
            NickName: {
                required: true,
                minlength: 2,
                remote : "user_availability.php",
            },
        },
        messages: {
             NickName:{
                      required: "<br>Please enter your username",
                      minlength : "<br>Your username must be at least 2 characters long",
                      remote: "<br>This username is not available, please try another"
                     }
        }
    });
});
</script>

When I try to add any addMethod regex examples found on here it will not work

this is what I have tried:

<script type="text/javascript">

jQuery().ready(function() {

jQuery.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            var check = false;
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        },
        "Please check your input."
);



    // validate signup form on keyup and submit
    jQuery("#regform").validate({
        rules: {
            NickName: {
                required: true,
                minlength: 2,
                remote : "user_availability.php",
                regex: "^[a-zA-Z'.\s]{1,40}$",
            },
        },
        messages: {
             NickName:{
                     required: "<br>Please enter your username",
                     minlength : "<br>Your username must be at least 2 characters long",
                     remote: "<br>This username is not available, please try another"
                     regex: "Invalid Character",
                    }    
        }
    });
});
</script>

please help


Your missing a ,

remote: "<br>This username is not available, please try another" <<<<<

and you dont need a ',' on the last element of your objects:

NickName: {
                required: true,
                minlength: 2,
                remote : "user_availability.php",
                regex: "^[a-zA-Z'.\s]{1,40}$",
            },   <<<<<<<

Personally i would make the whole think alot neater

var ValidationObject = {
    rules: {
        NickName: {
            required: true,
            minlength: 2,
            remote : "user_availability.php",
            regex: "^[a-zA-Z'.\s]{1,40}$",
        }
    },
    messages: {
        NickName:{
            required: "<br>Please enter your username",
            minlength : "<br>Your username must be at least 2 characters long",
            remote: "<br>This username is not available, please try another",
            regex: "Invalid Character",
        }
    }
}

Then use like so:

jQuery("#regform").validate(ValidationObject);
0

精彩评论

暂无评论...
验证码 换一张
取 消