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

时间:2010-05-31 08:33:39

标签: jquery

我使用过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 个答案:

答案 0 :(得分:50)

你可以尝试这样的事情:

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

答案 1 :(得分:10)

查看aditional methods,有nowhitespace规则。

答案 2 :(得分:7)

我使用了@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并不负责验证空TextBox(输入)。如果输入了某些内容,则负责检查值。

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

答案 3 :(得分:1)

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

onfocusout: function (element, event) {

    //Get Objects
    $element = $(element);

    //Remove whitespace
    if ( $element.is(':input') && !$element.is(':password') ) {
        $element.val($.trim($element.val()));
    }
},

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

答案 4 :(得分:1)

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

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

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

有关详细信息,请参阅Normalizer Docs

答案 5 :(得分:0)

像这样的例子:

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

答案 6 :(得分:0)

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

在验证中添加此方法

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