views:

442

answers:

5

I am validating 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 pretty well on add form validation but i use the same form for edit here they can use the same mobile no,but my plugin validates that mobileno saying there is already a mobileno... But how to execute remote attribute based on a condition,

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

Is this a valid jquery conditional validation statement.... How to skip remote attribute based on a condition....

A: 

You will need some server side. Either to handle the edit or to apply a different Url.Action.

To perform a differnet action to manage that it is editing the details not applying them in the same way, thus mobileno can be the same.

Good luck.

Edit Is it throwing back because of server side validation or does Jquery throw it back before it even gets there?

Thqr
A: 

Well, I would do something like this

for example my php page is member.php

to add, send a 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 is only going to apply if there is "add" in the url variable "action"

Starx
@Starx ya i ll defnitely try this...
Pandiya Chendur
A: 

What about using the jquery extend method? Add the remote property if it's a new record or use and empty object if it's and exiting 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"
       }
     }
Lee
A: 

You can imperatively remove rules after the validation rules have been initialized, using the rules('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');

That way you don't have to clutter up your shared declarative rules with inline logic. Even if #HfId doesn't exist on the add page, that will silently fall through without error (and without removing the remote rule, obviously), so it's applicable to both add and edit states.

Dave Ward
@Dave where should i include this if condition...
Pandiya Chendur
It just needs to be *after* the validate() method is called.
Dave Ward
A: 

I'm surprised nobody else has mentioned doing it this way, but I would suggest using the add rules method. After you've created your validation object, you can dynamically add rules to an object as needed.

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

The advantage of doing it this way is if you have multiple rules to add when inserting a new record, you can group them all in one place. You can also call add multiple times if your logic becomes more complex. I much prefer adding things when needed, then calling remove to take them away. You do this after calling validate().

ICodeForCoffee