Sheikh parvaz

Sheikh parvaz

  • NA
  • 199
  • 109.1k

Jquery server side validation in mvc

Mar 30 2017 3:04 AM
I am validating a name form serverside but it is shows me error message without validating and without calling conroller action method.
  1.  <script>  
  2.                 try {  
  3.                     var apiUrl = '@Url.Action("Validate", "ServerValidations")';  
  4.                     
  5.                     $('#formAdd').validate({  
  6.                         errorElement: 'span'//default input error message container  
  7.                         errorClass: 'help-block help-block-error'// default input error message class  
  8.                         focusInvalid: false// do not focus the last invalid input  
  9.                         ignore: "",  // validate all fields including form hidden input  
  10.                         messages: {  
  11.                             Name: {  
  12.                                 required: 'Please enter Name',  
  13.                                 maxlength: 'Length exceeded! Size should be less than 50 characters',  
  14.                                 VerifyName: "This Job title already exist in "  
  15.                             }  
  16.                         },  
  17.                         rules: {  
  18.                             ArName: {  
  19.                                 maxlength: 50,  
  20.                                 required: true,  
  21.                                 VerifyrName: true  
  22.                             }  
  23.   
  24.                         }  
  25.   
  26.   
  27.                     });  
  28.   
  29.                 } catch (err) {  
  30.                     //  alert(err.message);  
  31.                 }  
  32.   
  33.   
  34. function addVerifyNameRule(apiUrl, errMessage) {  
  35.     //alert('add rule :'+apiUrl);  
  36.     jQuery.validator.addMethod("VerifyName"function (value, element) {  
  37.         //alert('call validator');  
  38.         $.ajax({  
  39.             type: 'POST',  
  40.             dataType: 'json',  
  41.             async: false,  
  42.             url: apiUrl,  
  43.             data: { 'Value': value, 'ValidateRule': 1 },  
  44.             success: function (result) {  
  45.                 alert('res');  
  46.                 if (result['ValidateResult'] == '1') {  
  47.                     return true;  
  48.                     //alert('valid!');  
  49.                 }else  
  50.                 {  
  51.                     return false;  
  52.                     //alert('not valid!');  
  53.                 }  
  54.             },  
  55.             error: function (err) {  
  56.                 return false;  
  57.                 //alert('error : ' + err.message);  
  58.             }  
  59.         });  
  60.       //  return result;  
  61.     }, errMessage);  

 
 

Answers (4)