Wednesday, November 8, 2017

jquery validation plugin custome validation

Remote validation

****************************** *****************************************
$('form').validate({
    rules: {
          EmailID: {
                   required: true,
                   email: true,
                   remote: {
                       url: '/Patients/CheckEmailExist',
                       type: 'post',
                       // timeout: 2000,
                       data: {
                           EmailID: function () {
                               return $("#EmailID").val();
                           },
                           userID: function () {
                               return $("#PatientID").val();
                           }
                       }
                   },

               }
   }
});

 public ActionResult Patients(String EmailID,int userID)
    {
        if ((account.Email ?? string.Empty).Contains("oek"))
        {
            return Json(false, JsonRequestBehavior.AllowGet);
        }
        return Json(true, JsonRequestBehavior.AllowGet);
    }

=========Remote Validation using MVC=== ===============================

public class UserModel

    [Required]
    public string UserName { get; set; }
    [Remote("CheckExistingEmail","Home",ErrorMessage = "Email already exists!")]
    public string UserEmailAddress { get; set; }
}

public ActionResult CheckExistingEmail(string UserEmailAddress)
{
    bool ifEmailExist = false;
    try
   {
        ifEmailExist = UserEmailAddress.Equals("mukeshknayak@gmail.com") ? true : false;             
        return Json(!ifEmailExist, JsonRequestBehavior.AllowGet);
    }
   catch (Exception ex)
   {             
       return Json(false, JsonRequestBehavior.AllowGet);
    }

}










=========================================
  $('.rvwdate').rules("add", { CompareDates: true });

    $.validator.addMethod('CompareDates', function (value, element) {

        var odate = new Date(GetDate($('#txtOriginDate').val()));

        var rdate = new Date(GetDate($('#txtReviewDate').val()));

        if (rdate < odate) {
            return false;
        }
        else {
            return true;
        }

    }, jQuery.validator.format("Review Date Should be Greater Than Origin Date"));

  $.validator.setDefaults({ ignore: ":hidden:not(select)" });

=================================

No comments:

Post a Comment

javascript Filter/index off

 var family = [{"name":"Jack",  "age": 26},               {"name":"Jill",  "age"...