jquery 表单验证不允许用户名字段的空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2942325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:35:38  来源:igfitidea点击:

jquery form validate not allow space for username field?

jquery

提问by kn3l

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

我用过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

谢谢

回答by Reigel

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.

快速演示
更多关于 addMethod 在这里
和一个例子也在这里。

回答by Orlando

Take a look to aditional methods, there is a nowhitespacerule.

看看其他方法,有一个nowhitespace规则。

回答by Ardalan Shahgholi

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

我使用了@Reigel 的答案,但它对我不起作用。

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.

在我看来 noSpace 不负责验证空的 TextBox(Input)。如果输入了一些东西,它负责检查值。

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.

如果您jQuery.validator.addMethod...在其他 JS 文件中使用并将此 JS 文件添加到您的母版页或类似的东西,那将是一个好主意。

回答by Roy Shoa

I just add the onfocusoutevent 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事件,它会自动修剪所有表单字段,如果用户只输入空格(空格),它将删除它们并显示需要字段的消息:

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.

当用户直接提交表单时它也能工作,因为当他点击提交按钮时会触发事件。

回答by Nic Nilov

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

从 jquery-validation 1.15.0 开始,更简洁的方法是使用 normalizer 回调:

$( "#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 Docsfor more details.

有关更多详细信息,请参阅规范器文档

回答by gauravbhai daxini

$.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
      }
   }
});

回答by Amit Meena

example like this :

像这样的例子:

   $('#username').keypress(function( e ) {
       if(e.which === 32) 
         return false;
    });