适应用户名可用性检查代码

时间:2017-01-21 09:57:40

标签: passwords

我需要您的帮助来调整显示用户名已经显示的代码。

此代码正常运行:

$(document).ready(function()
{   
 	$("#name").keyup(function()
 	{ 
  	  var name = $(this).val();
   
	  $.post( "username-check.php", { name: $("#name").val() }, function (data){
          if(data=='1'){
          //the php post result is equal to '1' if the username is already taken.
	  // This actualy display "1" in the form if the username is taken.
	  $("#result").html(data);
       }
    }); //function(data)
  });
})

我从网站上获取此代码,以检查并验证表单的输入

$('document').ready(function()
{   
   // Check and validate the username input
   var nameregex = /^[a-zA-Z0-9 ]+$/;
   
   $.validator.addMethod("validname", function( value, element ) {
   return this.optional( element ) || nameregex.test( value );
   }); 
 
 /* To check if the username is taken I think I have to add something like that :
    var checkusernameregex = ??????;
    
   $.validator.addMethod("checkusername", function( value, element ) {
   return this.optional( element ) || checkusernameregex.test( value );
   }); */
 
   $("#register-form").validate({
      
    rules:
    {
     name: {
     required: true,
     validname: true,
   //chekusername : true,
     minlength: 5, 
     maxlength: 25
     }
    },
    messages:
    {
     name: {
     required: "Please choose a username",
     validname: "Username can only have alphanumerical characters ",
     //checkusername: "Username already taken";
     minlength: "Username is too short"
      }
    },
 });

//显示错误代码,但我的实际请求不需要 谢谢

0 个答案:

没有答案