How to conditionally execute jquery validation?

I validate the form using jquery validation plugin ......

rules: { Name: "required", MobileNo: { required: true, minlength: 10, remote: '<%=Url.Action("getClientMobNo", "Clients") %>' }, Address: "required" }, messages: { Name: "please provide a client name", MobileNo: { required: "Please provide a mobile phone no", rangelength: jQuery.format("Enter at least {0} characters"), remote: jQuery.format("This MobileNo is already in use") }, Address: "please provide client address" }, 

This works well when adding form validation, but I use the same form for editing, here they can use the same mobile number, but my plugin confirms that mobileno says that it is already mobile ... But how to execute the remote attribute on basis provided

  MobileNo: { required: true, minlength: 10, if($("#HfId").val() == ""){ remote: '<%=Url.Action("getClientMobNo", "Clients") %>' } }, 

Is this a valid conditional condition for jQuery conditional checking .... How to skip a remote attribute based on a condition ....

+4
source share
5 answers

Ok i would do something like this

e.g. my php page is member.php

add, send url like this member.php? action = add

 <? $action = $_GET['action']; ?> <script> $("#frmmember").validate({ rules: { name: { <? if($action=='add') { ?> required: true, <? } ?> rangelength: [4, 50] }, email: { required: true, rangelength: [5, 50], email: true }, phone: { required: true, number:true, rangelength: [7, 10] } }, onkeyup: false }); </script> 

In this case, the validation rule required = true will only apply if the url variable has "action"

0
source

You will need the server side. Either handle editing, or apply another Url.Action.

In order to execute the differnet action in order to control that it edits the parts without applying them in the same way, thus mobileno can be the same.

Good luck.

Edit Is it dropped due to server-side validation or is Jquery dropping it back before it gets there?

0
source

How about using jquery extension method? Add a remote property if it is a new record or if it uses an empty object if it also exits the record.

  rules: { Name: "required", MobileNo: $.extend( { required: true, minlength: 10 }, ($("#HfId").val() == "")?{remote: '<%=Url.Action("getClientMobNo", "Clients") %>'}:{} ), Address: "required", messages: { Name: "please provide a client name", MobileNo: { required: "Please provide a mobile phone no", rangelength: "Enter at least {0} characters", remote: "This MobileNo is already in use" }, Address: "please provide client address" } } 
0
source

You can force the removal of rules after initializing the validation rules using the ('remove') method.

 $('#yourForm').validate({ rules: { Name: "required", MobileNo: { required: true, minlength: 10, remote: '<%=Url.Action("getClientMobNo", "Clients") %>' }, Address: "required" }, messages: { Name: "please provide a client name", MobileNo: { required: "Please provide a mobile phone no", rangelength: jQuery.format("Enter at least {0} characters"), remote: jQuery.format("This MobileNo is already in use") }, Address: "please provide client address" } }); if ($('#HfId').val() !== '') $('#MobileNo').rules('remove', 'remote'); 

Thus, you do not need to clutter your general declarative rules with inline logic. Even if #HfId does not exist on the add page, it will fail without errors (and without deleting the deleted rule, obviously), so it applies to both the add and edit states.

0
source

I am surprised that no one mentioned it that way, but I would suggest using the add rules method. Once you have created your validation object, you can dynamically add rules to the object as needed.

  if($("#HfId").val() == "") { $("#MobileNo").rules("add", { remote: '<%=Url.Action("getClientMobNo", "Clients") %>' }); } 

The advantage of this method is that when adding a new record when adding several rules, you can group them in one place. You can also call adding a few times if your logic becomes more complex. I prefer to add things as needed and then call delete to take them away. You do this after calling validate() .

0
source

Source: https://habr.com/ru/post/1310699/


All Articles