Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery form validate not allow space for username field?

Tags:

jquery

I have used http://bassistance.de/jquery-plugins/jquery-plugin-validation/

my form validate :

$("#form_person").validate({
    rules: {


        username: {
            required: true,
            minlength: 2,
            maxlength:15
        },
        password: {
            required: true,
            minlength: 2
        },
        confirm_password: {
            required: true,
            minlength: 2,
            equalTo: "#password"
        },
        email: {
            required: true,
            email: true
        }

    },
    messages: {

        username: {
            required: "Please enter a username",
            maxlength:"max length 15 digits",
            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"
        },
        confirm_password: {
            required: "Please provide a confirm password",
            minlength: "Your password must be at least 5 characters long",
            equalTo: "Please enter the same password as above"

    }
});

Anybody help me to validate not allow space on username?

thanks

like image 760
kn3l Avatar asked May 31 '10 08:05

kn3l


1 Answers

you can try something like this:

$(document).ready(function(){

  jQuery.validator.addMethod("noSpace", function(value, element) { 
  return value.indexOf(" ") < 0 && value != ""; 
}, "No space please and don't leave it empty");


$("form").validate({
   rules: {
      name: {
          noSpace: true
      }
   }
  });


})

quick demo
more on addMethod here
and an example also here.

like image 186
Reigel Avatar answered Sep 29 '22 12:09

Reigel