2014-02-18 79 views
0

我遇到問題,不知道表單如何接受用戶名,例如沒有電子郵件擴展名,例如我只能在「[email protected]」中輸入「pro」Javascript代碼用戶登錄沒有電子郵件分機

這裏是我的代碼:

function loginHI(menu){ 
    $(".hopplerTextboxLogin").focusin(function(){ 
     hopplerTextboxFocused($(this)); 
    }).focusout(function(){ 
     $(this).removeClass("hopplerTextboxBorderBlue").addClass("hopplerTextbox"); 
    }); 

    $("#loginForm input").keyup(function(e){ 
     if(e.keyCode == 13){ 
      $("#btnLogin").click(); 
     } 
    }); 

    $("#btnLogin, #btnLogin2").click(function(){ 

     //variable for email 
     var get_email = $("#j_username").val(); 
     var get_pass = $("#j_password").val(); 

     //validation 
     if(get_email=="" && get_pass==""){ 
      somethingIsWrongHere("divUsername_login","Required fields"); 
      somethingIsWrongHere("divPassword_login","Required fields"); 
      return false; 
     } 

     if(get_email=="" || !isValidEmailAddress(get_email)){ 
      somethingIsWrongHere("divUsername_login","Invalid email address"); 
      return false; 
     } 

     if(get_pass == ""){ 
      somethingIsWrongHere("divPassword_login","Password is required"); 
      return false; 
     } 

     var result_page = setProjectName + "/j_spring_security_check"; 

     $("#loginForm_message").html("<div class=\"loading_message_login\">logging in...</div>").fadeIn(); 
     $("#cboxContent").removeClass("cboxContentHeight").addClass("cboxContentHeightMessage"); 
     $.ajax({ 
      type: 'POST', 
      url: result_page, 
      data: $("#loginForm").serialize(), 
      beforeSend: function (xhr) { 
       xhr.setRequestHeader("X-Ajax-call", "true"); 
      }, 
      success: function(data) { 
       if(data.result==1){ 
        localStorage.shortlists = ""; 
        var sl_array = []; 
        localStorage.userFullName = data.firstName+" "+data.lastName; 
        var user_url = "HICollection3/select?q=id%3A"+data.userId+"&wt=json&indent=true"; 
        var sl_url = "ShortList_Collection/select?q=removed%3Afalse+AND+user_id%3A"+data.userId+"&rows=1000&wt=json&indent=true&json.wrf=?"; 
        $.get("properties/querySolr",{url : sl_url},function(result) { 
         solr_results = result; 
         var obj = $.parseJSON(solr_results); 
         console.log("obj.numFound" + obj.numFound); 
         var sl = obj.docs; 
          $.each(sl,function(index){ 
            sl_array.push(sl[index].property_id); 
          }); 
          localStorage.shortlists = sl_array; 
          /*EMBEDDED HIDDENLIST FXN*/ 
          var hl_array = []; 
          var hl_url = "HiddenList_Collection/select?q=user_id%3A"+data.userId+"&rows=1000&wt=json&indent=true&json.wrf=?"; 
          $.get("properties/querySolr",{url : hl_url},function(result2) { 
            solr_results2 = result2; 
            var obj2 = $.parseJSON(solr_results2); 
            console.log("obj2.numFound" + obj2.numFound); 
            var hl = obj2.docs; 
            $.each(hl,function(index){ 
             hl_array.push(hl[index].property_id); 
            }); 
            localStorage.hiddenlists = hl_array; 
            $.get("properties/querySolr",{url : user_url},function(result3) { 
             var solr_results3 = result3; 
             var obj3 = $.parseJSON(solr_results3); 
             console.log("obj3.numFound" + obj3.numFound); 
             var user_res = obj3.docs; 
             localStorage.userContact=""; 
             localStorage.userFullName=""; 
             localStorage.userEmail=""; 
              $.each(user_res,function(index){ 
                localStorage.userFullName = user_res[index].firstName+" "+user_res[index].lastName; 
                localStorage.userEmail = user_res[index].email; 
                if(user_res[index].mobile!=undefined || user_res[index].mobile!="") 
                 localStorage.userContact = user_res[index].mobile; 
                else if(user_res[index].telephone!=undefined || user_res[index].telephone!="") 
                 localStorage.userContact = user_res[index].telephone; 
                else 
                 localStorage.userContact = ""; 
              }); 
              setTimeout(function(){ 
               location.reload(); 
              },1000); 

            }); 

          }); 
        }); 

       } 
       else{ 
        somethingIsWrongHere("",data.result); 
       } 
      } 
     }); 

     return false; 
    }); 

    $("#keepMeLoggedIn").click(function(){ 
     $("#rememberMe").trigger("click"); 
    }); 
} 

用戶登錄也應該接受整個電子郵件地址。 這是isValidEmailAddress功能

function isValidEmailAddress(emailAddress) { 
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i); 
return pattern.test(emailAddress); 

}的代碼;

+0

我的第一個猜測是'isValidEmailAddress'工作不正常。但是,它看起來像你的例子中缺少的代碼,所以很難從這裏幫助你。 –

+0

嗨,brandon我已經編輯它。 isValidEmailAddress的代碼已經提供。 – user2952007

+0

@ user2952007這個問題有可能在jsbin或jsfiddle上得到一個工作原型嗎?我測試了電子郵件驗證功能,它可以工作,所以它可能是其他的東西。 – Joseph

回答

0

再一次...您將使用的功能測試將取決於您希望允許的字符。如果你想只允許字母和數字,你可以使用...

function isValidUsername(username) { 
    var pattern = new RegExp(/^[a-zA-Z0-9]+$/); 
    return pattern.test(username); 
} 

,如果你想允許時期和@符號,這樣你可以有作爲用戶名作爲可選的電子郵件...

function isValidUserName(username) { 
    var pattern = new RegExp(/^[a-zA-Z0-9\.\@]+$/); 
    return pattern.test(username); 
} 

請注意,如果您使用第二個功能,用戶可以使用字母數字和@符號和句點的任意組合。因此,他們可以輸入@@@ ...或[email protected]或adfsadf8347

相關問題