jquery - Javascript space function - Stack Overflow

I have a JS code that the function is to validate the form, now what I want to do is add 1 function to

I have a JS code that the function is to validate the form, now what I want to do is add 1 function to prevent space in textbox username with message too, "No space allowed". Here it's my JS code so far :

$().ready(function()
    {
        $("#signupForm").validate(
        {
            rules:
            {
                full_name: "required",
                username: {
                    required: true,
                    minlength: 2
                },
                password: {
                    required: true,
                    minlength: 5
                },
                email: {
                    required: true,
                    email: true
                },
            },
            messages:
            {
                full_name: "Please enter your full name",
                username:
                {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters"
                },

                password:
                {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },

                email: "Please enter a valid email address"
            }
        });
    });

Anyone can give me idea ? Thanks.

I have a JS code that the function is to validate the form, now what I want to do is add 1 function to prevent space in textbox username with message too, "No space allowed". Here it's my JS code so far :

$().ready(function()
    {
        $("#signupForm").validate(
        {
            rules:
            {
                full_name: "required",
                username: {
                    required: true,
                    minlength: 2
                },
                password: {
                    required: true,
                    minlength: 5
                },
                email: {
                    required: true,
                    email: true
                },
            },
            messages:
            {
                full_name: "Please enter your full name",
                username:
                {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters"
                },

                password:
                {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },

                email: "Please enter a valid email address"
            }
        });
    });

Anyone can give me idea ? Thanks.

Share edited Sep 24, 2012 at 7:31 Niklas 13.1k23 gold badges82 silver badges122 bronze badges asked Sep 24, 2012 at 7:29 Naga BotakNaga Botak 7512 gold badges9 silver badges14 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Try this


Add a method called noSpace

jQuery.validator.addMethod("noSpace", function(value, element) {
    return !value.match(/\s/g);
}, "Spaces are not allowed");

Set it as a rule for username

rules: {
    // ...
    username: {
        // ...
        noSpace: true,
        // ...
    },
    // ...

Set a custom message, if you want to override the default message

messages: {
    // ...
    username: {
        // ...
        noSpace: "Username should not contain spaces",
        // ...
    },
<script type="text/javascript">
function nospaces(t) {
    if(t.value.match(/\s/g)) {
        alert('Sorry, you are not allowed to enter any spaces');
    }
}
</script>

Add following attribute to text input box

onkeyup="nospaces(this)"
jQuery.validator.addMethod("noSpace", function(value, element) { 
    return value.indexOf(" ") < 0 && value != ""; 
}, "No space allowed");

$("#signupForm").validate({
    rules: {
        username:
           {
              required: "Please enter a username",
              minlength: "Your username must consist of at least 2 characters",
              noSpace: true
           }
     }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744911153a4600558.html

相关推荐

  • jquery - Javascript space function - Stack Overflow

    I have a JS code that the function is to validate the form, now what I want to do is add 1 function to

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信