Search code examples
phpjquerycodeignitersmarty

i want to log in a user with enter key


I want to log in a user by pressing the enter key. when I press the enter key it clears the form value. and by clicking with the mouse on the sign in button it allows the user to log in and redirect on the home page. here is my login.min.js

var Login = function () {
var e = function () {
    $(".login-form").validate({
        errorElement: "span",
        errorClass: "help-block",
        focusInvalid: !1,
        rules: {username: {required: !0}, password: {required: !0}, remember: {required: !1}},
        messages: {username: {required: "Username is required."}, password: {required: "Password is required."}},
        invalidHandler: function (e, r) {
            $(".alert-danger", $(".login-form")).show()
        },
        highlight: function (e) {
            $(e).closest(".form-group").addClass("has-error")
        },
        success: function (e) {
            e.closest(".form-group").removeClass("has-error"), e.remove()
        },
        errorPlacement: function (e, r) {
            e.insertAfter(r.closest(".input-icon"))
        },
        submitHandler: function (e) {
            e.submit()
        }
    }), $(".login-form input").keypress(function (e) {
        return 13 == e.which ? ($(".login-form").validate().form() && $(".login-form").submit(), !1) : void 0
    })
}, r = function () {
    $(".forget-form").validate({
        errorElement: "span",
        errorClass: "help-block",
        focusInvalid: !1,
        ignore: "",
        rules: {email: {required: !0, email: !0}},
        messages: {email: {required: "Email is required."}},
        invalidHandler: function (e, r) {
        },
        highlight: function (e) {
            $(e).closest(".form-group").addClass("has-error")
        },
        success: function (e) {
            e.closest(".form-group").removeClass("has-error"), e.remove()
        },
        errorPlacement: function (e, r) {
            e.insertAfter(r.closest(".input-icon"))
        },
        submitHandler: function (e) {
            e.submit()
        }
    }), $(".forget-form input").keypress(function (e) {
        return 13 == e.which ? ($(".forget-form").validate().form() && $(".forget-form").submit(), !1) : void 0
    }), jQuery("#forget-password").click(function () {
        jQuery(".login-form").hide(), jQuery(".forget-form").show()
    }), jQuery("#back-btn").click(function () {
        jQuery(".login-form").show(), jQuery(".forget-form").hide()
    })
}, i = function () {
    function e(e) {
        if (!e.id)return e.text;
        var r = $('<span><img src="../assets/global/img/flags/' + e.element.value.toLowerCase() + '.png" class="img-flag" /> ' + e.text + "</span>");
        return r
    }

    jQuery().select2 && $("#country_list").size() > 0 && ($("#country_list").select2({
        placeholder: '<i class="fa fa-map-marker"></i>&nbsp;Select a Country',
        templateResult: e,
        templateSelection: e,
        width: "auto",
        escapeMarkup: function (e) {
            return e
        }
    }), $("#country_list").change(function () {
        $(".register-form").validate().element($(this))
    })), $(".register-form").validate({
        errorElement: "span",
        errorClass: "help-block",
        focusInvalid: !1,
        ignore: "",
        rules: {
            fullname: {required: !0},
            email: {required: !0, email: !0},
            address: {required: !0},
            city: {required: !0},
            country: {required: !0},
            username: {required: !0},
            password: {required: !0},
            rpassword: {equalTo: "#register_password"},
            tnc: {required: !0}
        },
        messages: {tnc: {required: "Please accept TNC first."}},
        invalidHandler: function (e, r) {
        },
        highlight: function (e) {
            $(e).closest(".form-group").addClass("has-error")
        },
        success: function (e) {
            e.closest(".form-group").removeClass("has-error"), e.remove()
        },
        errorPlacement: function (e, r) {
            "tnc" == r.attr("name") ? e.insertAfter($("#register_tnc_error")) : 1 === r.closest(".input-icon").size() ? e.insertAfter(r.closest(".input-icon")) : e.insertAfter(r)
        },
        submitHandler: function (e) {
            e[0].submit()
        }
    }), $(".register-form input").keypress(function (e) {
        return 13 == e.which ? ($(".register-form").validate().form() && $(".register-form").submit(), !1) : void 0
    }), jQuery("#register-btn").click(function () {
        jQuery(".login-form").hide(), jQuery(".register-form").show()
    }), jQuery("#register-back-btn").click(function () {
        jQuery(".login-form").show(), jQuery(".register-form").hide()
    })
};
return {
    init: function () {
        e(), r(), i()
    }
}
}();
jQuery(document).ready(function () {
Login.init()
});

and my login_form.tpl

<body class=" login">
    <!-- BEGIN LOGO -->
    <div class="logo">
        <a href="#">
       <img src=" {$site_root}application/views/admin/assets/layouts/layout/img/logo.png" 
alt="logo" /> </a>
    </div>
    <!-- END LOGO -->
    <!-- BEGIN LOGIN -->
    <div class="content">
        <!-- BEGIN LOGIN FORM -->
        <form class="login-form" action="{$data.action}" method="post">
            <h3 class="form-title font-green">Sign In</h3>
                <div class="alert alert-danger display-hide" style='display:{if $error != ''}block{/if}'>
                    <button class="close" data-close="alert"></button>
                    <span> {if $error != ''}{$error}{else}Enter any username and password.{/if}</span>
                </div>
            <div class="form-group">
                <!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
                <label class="control-label visible-ie8 visible-ie9">{l i='field_nickname' gid='ausers'}</label>
                <input class="form-control form-control-solid placeholder-no-fix" type="text" autocomplete="off" placeholder="Username" name="nickname" /> </div>
            <div class="form-group">
                <label class="control-label visible-ie8 visible-ie9">{l i='field_password' gid='ausers'}</label>
                <input class="form-control form-control-solid placeholder-no-fix" type="password" autocomplete="off" placeholder="Password" name="password" /> </div>
            <div class="form-actions">
                <input type="submit" name="btn_login" value="{l i='btn_login' gid='start' type='button'}" class="btn green uppercase btn-block">
            </div>
        </form>

i have tried different solutions but did'nt succeed.


Solution

  • Thank you for your help but finally, I find the solution I just needed to make some changes.

     $("input").keypress(function (e) {
            if (e.which == 13) {
                $('input[name="btn_login"]').click();
                return false;
            }