March 18, 2013

jQuery syntax error?

Question by user1977591

I found an example here on stackoverflow that I am trying to recreate and use a solution.
here is the link to the example: Refreshing Partial View in MVC 3

what is wrong with my syntax?

function RefreshPartial() {
        $('#invited-teams').load('/Challenge/GetInvitedTeams', {Model.ChallengeId});
    }   

will this work? or does it need to have the curly brackets?

function RefreshPartial() {
        $('#invited-teams').load('/Challenge/GetInvitedTeams', "<%= Model.ChallengeId %>" );
    }   

UPDATE:

 function RefreshPartial() {
        $('#invited-teams').load('/Challenge/GetInvitedTeams',
            {'paramname' : <%:Model.ChallengeId %> });
    }   

my partial view:

            <div id="invited-teams">
                <% Html.RenderPartial("InvitedTeams", Model.InvitedTeams); %>
            </div>

So, this is what my refreshPartial method is looking like:

function RefreshPartial() {
        alert("in refresh partial");
        alert("<%:Model.ChallengeId %>");
        $('#invited-teams').load('/Challenge/GetInvitedTeams', { 
            'paramname': '<%:Model.ChallengeId %>'
        });


    }

it alerts both alerts correctly, with the 2nd one having the correct ChallengeID. The page is still going blank though. hmm..

Answer by Starx

Your function does not specify a parameter name.

function RefreshPartial() {
    $('#invited-teams').load('/Challenge/GetInvitedTeams', {
       'paramname' :'<%:Model.ChallengeId %>'//Or your ASP wrapper here
    });
}  
March 23, 2012

Problems with my javascript, I dont want to validate <select> tags that are disabled

Question by ps__

This javascript will find all my selects and validate them but I want to add that it doesnt validate my disabled selects. How can I code that in my javascript?

$("#next-step").click(function () {

                var $step = $(".wizard-step:visible"); // get current step

                var validator = $("form").validate(); // obtain validator
                var anyError = false;
                $step.find("select").each(function () {
                    if (!validator.element(this)) { // validate every input element inside this step
                        anyError = true;
                    }


                });

                if (anyError)
                    return false; // exit if any error found

Thanks in advance!

Answer by ehynds

if (!this.disabled && !validator.element(this)) {
    anyError = true;
}​

or (slower)

$step.find("select:enabled").each(function() {
    if (!validator.element(this)) {
        anyError = true;
    }
});​

Answer by Starx

The easiest way, i know of is to give the select tags, a class like skipvalidation and then

$("form").validate(
   ignore: ".skipvalidation"
);

You can find all the disabled elements and add the class as

$("input:disabled").addClass("skipValidation");
$("form").validate(
   ignore: ".skipvalidation"
);
June 2, 2010

How to conditionally execute a jquery validation?

Question by Chendur Pandian

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….

Answer by Starx

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"

...

Please fill the form - I will response as fast as I can!