Link to home
Start Free TrialLog in
Avatar of Boopathy S
Boopathy SFlag for India

asked on

Need to come scroll up for validation using Jquery

I have created for validation error using Jquery. The errors are working fine. But its not showing as a scroll up mode. I need to manually scroll up and seeing the error. I have tried .offset().top type. But its not working. I'm new to the Jquery. Can anyone please help me on this.

My Jquery file is:

var apiHost = "";
var apiResUrl = "/v1/resources";
var apiAddNewEmployee = "/addemployee";
var apiGetEmployees = "/getemployees";

var addEmpValidation = function() {
    var allowedNameLetters = /^[A-Z a-z]+$/;
    var fname = $("#fname").val();
    var lname = $("#lname").val();
    if (fname == undefined || fname == "" || !fname.match(allowedNameLetters)) {
        $(".newEmperrorMSG").text("Please Fill Valid Employee First Name !!!");
        $(".newEmperrorMSG").show();
    }        
    else if (lname == undefined || lname == "" || !lname.match(allowedNameLetters)) {
        $(".newEmperrorMSG").text("Please Fill Valid Employee Last Name !!!");
        $(".newEmperrorMSG").show();
    }
    else {
        addEmployee();
        $('#new-employee').modal('hide');
    }
};

var loadEmployee = function() {
$('#dashSubBtn').on('click', function(e) {
        e.preventDefault();
        addEmpValidation();
    });
    };

var addEmployee = function() {
    var data = $(newEmpForm).serializeArray();
    $(".successMSG").hide();
    $(".errorMSG").hide();
    $.ajax({
        url: apiHost + apiResUrl + apiAddNewEmployee,
        type: 'POST',
        data: JSON.stringify(data),
        headers: { token: $.cookie('token') },
        contentType: "application/json",
        error: function(err) {
            logoutFromApp();
        },
        success: function(data) {
            if (!isEmpty(data.response.status) && data.response.status.toLowerCase() == "invalid") {
                logoutFromApp();
            } else {
                if ((data.response.message != undefined) || (data.response.message != "")) {
                    $(".successMSG").text(data.response.message);
                    $(".successMSG").show();
                }
                searchEmployees(false);
                $(".successMSG").delay(3000).fadeOut(2000);
                $(".errorMSG").delay(3000).fadeOut(2000);
            }
        }
    });
};

Open in new window


In that above, I have write the code for employee portal as like first name and last name.

Html file I have wrote for this as:
<div class="modal fade" id="new-employee" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Add New Employee</h4>
            </div>
            <div class="modal-body">
                <form id="newEmpForm" class="form-horizontal">
                    <span class="alert alert-danger newEmperrorMSG" style="display:none"></span>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">Employee ID <span class="asterisk">*</span></label>
                        <div class="col-sm-9">
                            <input type="text" name="empID" id="empID" class="form-control" readonly="readonly" placeholder="Employee ID..." required />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">Emp First Name <span class="asterisk">*</span></label>
                        <div class="col-sm-9">
                            <input type="text" name="fname" class="form-control" id="fname" placeholder="Employee First Name..." required />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">Emp Last Name <span class="asterisk">*</span></label>
                        <div class="col-sm-9">
                            <input type="text" name="lname" class="form-control" id="lname" placeholder="Employee Last Name..." required />
                        </div>
                    </div>
                    <!-- panel-footer -->
                    <div class="panel-footer">
                        <div class="row">
                            <div class="col-sm-9 col-sm-offset-3">
                                <button class="btn btn-primary" id="dashSubBtn">Add</button>
                                <button type="reset" class="btn btn-default" id="dashResetBtn">Reset</button>
                            </div>
                        </div>
                    </div>
                    <!-- panel -->
                </form>
            </div>
        </div>
        <!-- modal-content -->
    </div>
    </div>

Open in new window


What my problem is, the error message is showing in that upside. Needs to scroll up manually and have to see. I want the error needs to show as automatic scroll up and show the error. Is there is having any ways to make the automatic scroll up using my code. Because i tried .offset().top, but its not working on my machine. Please anyone provide me the suggestion. Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial