没有电子邮件扩展的Javascript代码用户登录

时间:2014-02-18 02:09:21

标签: javascript jquery html5 forms validation

我遇到了问题,并且不知道表单如何接受没有电子邮件扩展名的用户名,例如我只能在“pro@domain.com”中输入“pro”

这是我的代码:

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);

};

1 个答案:

答案 0 :(得分: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);
}

请注意,如果您使用第二个功能,用户可以使用字母数字和@符号以及句点的任意组合。因此,他们可以输入@@@ ...或ddd@example.com或adfsadf8347