代码之家  ›  专栏  ›  技术社区  ›  tree em

jquery表单validate不允许用户名字段有空间?

  •  15
  • tree em  · 技术社区  · 15 年前

    我已经用过 http://bassistance.de/jquery-plugins/jquery-plugin-validation/

    我的表单验证:

    $("#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"
    
        }
    });
    

    有人帮我验证用户名上不允许有空格吗?

    谢谢

    7 回复  |  直到 6 年前
        1
  •  47
  •   Reigel Gallarde    15 年前

    您可以尝试如下操作:

    $(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.

        2
  •  9
  •   Orlando    8 年前

    看一看 aditional methods 有一个 nowhitespace 规则。

        3
  •  5
  •   Ardalan Shahgholi    8 年前

    我用了@reigel的答案,但不适用于我。

    我像这样改变了他的例子:

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

    在我看来,nospace不负责验证空文本框(input)。如果输入了某些内容,则负责检查值。

    如果你使用 jQuery.validator.addMethod... 在其他JS文件中,并将此JS文件添加到母版页或类似的内容中。

        4
  •  1
  •   Roy Shoa    8 年前

    我只是添加了 onfocusout 事件及其自动修剪所有表单字段,并且如果用户只输入空格(空格),它将删除这些字段并显示字段所需消息:

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

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

        5
  •  1
  •   Nic Nilov    7 年前

    由于jquery validation 1.15.0,更清洁的方法是使用规范化器回调:

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

    规范化器(不变)在验证发生之前转换值。

    Normalizer Docs 了解更多详细信息。

        6
  •  0
  •   Amit Meena    7 年前

    示例如下:

       $('#username').keypress(function( e ) {
           if(e.which === 32) 
             return false;
        });
    
        7
  •  0
  •   Gauravbhai Daxini    6 年前
    $.validator.addMethod("validUsername", function (value, element) {
        return /^[a-zA-Z0-9_.-]+$/.test(value);
    }, "Please enter a valid username");
    

    在验证中添加此方法

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