Question

I have 2 textboxes..

  • Email

  • Mobile Number

I check the 2 fields with database by link using JQuery. When I enter the values in the textbox, it should automatically check the value and show a popup saying 'already registered' and also get the related values..

My code is

<script type ="text/javascript">
                $('#getCandidate').text('Get Profile') // Sets text for email.
                    .attr('href', '#');

                $("#getCandidate").click(function () {

                    $('#getCandidate').text('Get Profile') 
                    .attr('href', 'GetCandidateDetail?validateEmail=' + $('#Email').val());
                });

                $(document).ready(function () {
                    $('#checkEmail').click(function () {
                        var name = $('#Email').val();
                        var data = 'validateEmail=' + name;
                        $.ajax({
                            type: "GET",
                            url: "ValidateCandidate",
                            data: data,
                            success: function (data) {
                                   alert(data);

                            }
                        });
                        return false;
                    });
                });
          </script>

Controller:

 public ActionResult GetCandidateDetail(string validateEmail, string validateMobile)
    {

        Candidate candidate = null;

        if (!string.IsNullOrEmpty(validateEmail))
        {
            candidate = _userRepository.GetCandidateByEmail(validateEmail);
            if (candidate == null)
               candidate = new Candidate();
        }
        else if (!string.IsNullOrEmpty(validateMobile))
        {
            candidate = _userRepository.GetCandidateByMobileNumber(validateMobile);
            if (candidate == null)
                candidate = new Candidate();
        }
        else
        {
            if (candidate == null)
                candidate = new Candidate();
        }
... 

How to automatically check in JQuery, without clicking the link. I tried the blur function but in vain.

Was it helpful?

Solution

You can trigger the chekEmail link on change or blur depends on your need

$('#Email').blur(function() {
    $('#checkEmail').trigger('click'); //it will trigger the link when the text losses focus
});

OR

$('#Email').change(function() {
    $('#checkEmail').trigger('click');  //it will trigger the link each time when the text gets changed 
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top