19

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

asked May 31, 2010 at 8:33
5
  • I think required: true, is already in that... Commented May 31, 2010 at 8:39
  • no it allow space.More you can test here jquery.bassistance.de/validate/demo Commented May 31, 2010 at 8:41
  • tested the link and yes it work as expected...it say's This field is required. when I inputted spaces and hit submit button... Commented May 31, 2010 at 9:03
  • ahhh I think you mean, accept reigel as input but not reigel gallarde cause it has a space in between?.. Commented May 31, 2010 at 9:04
  • for example : username: Reigel >>ok username: Rei gel >>Not allow Commented May 31, 2010 at 9:08

7 Answers 7

63

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.

answered May 31, 2010 at 9:17
Sign up to request clarification or add additional context in comments.

2 Comments

Thankssss so much,this is really what i am looking for,
but I am using "required:true" also so it displays "please enter a value" msg instead of "No space please and don't leave it empty";
12

I used the answer of @Reigel but it didn't work for me.

I have change his example like this :

 $(document).ready(function(){
 jQuery.validator.addMethod("noSpace", function(value, element) { 
 return value == '' || value.trim().length != 0; 
 }, "No space please and don't leave it empty");
 $("form").validate({
 rules: {
 name: {
 noSpace: true
 }
 }
 });
 })

In my opinion noSpace isn't responsible to verify empty TextBox(Input). It is responsible to check value if some things was entered.

And it will be a good idea if you use jQuery.validator.addMethod... in other JS file and add this JS file to your master page or some things like that.

answered Sep 14, 2016 at 18:07

Comments

12

Take a look to aditional methods, there is a nowhitespace rule.

answered Nov 15, 2016 at 5:06

Comments

2

I just add the onfocusout event and its automatically Trim all the form fields and also if the user will enter only spaces (white-spaces) it will remove them and will show the field required message:

onfocusout: function (element, event) {
 //Get Objects
 $element = $(element);
 //Remove whitespace
 if ( $element.is(':input') && !$element.is(':password') ) {
 $element.val($.trim($element.val()));
 }
},

Its working also when the user submit the form directly because when he click the submit button the event is triggered.

answered Sep 20, 2016 at 12:25

Comments

2

Since jquery-validation 1.15.0 a cleaner approach is to use the normalizer callback:

$( "#myform" ).validate( {
 rules: {
 field: {
 required: true,
 normalizer: function( value ) {
 return $.trim( value );
 }
 }
 }
} );

The normalizer (immutably) transforms the value before the validation takes place.

See the Normalizer Docs for more details.

answered Nov 3, 2017 at 18:07

Comments

1

example like this :

 $('#username').keypress(function( e ) {
 if(e.which === 32) 
 return false;
 });
answered Sep 5, 2017 at 12:43

Comments

1
$.validator.addMethod("validUsername", function (value, element) {
 return /^[a-zA-Z0-9_.-]+$/.test(value);
}, "Please enter a valid username");

Add this method in validation

$("form").validate({
 rules: {
 name: {
 validUsername: true
 }
 }
});
answered Oct 25, 2018 at 12:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.