Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7821031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:31:04+00:00 2026-06-02T07:31:04+00:00

Description: I have a simple form in an MVC4 application that has 5 textboxes

  • 0

Description:

I have a simple form in an MVC4 application that has 5 textboxes named loc1-5 and a submit button. The application takes up to 5 addresses in the textboxes loc1-5 and uses the bing geocode services with jQuery to do some processing on the addresses and create a map with directions.

The issue is that I need to validate the loc1-5 textboxes to ensure that they are valid addresses before continuing and decided that the best way that makes sense is to use jQuery.validate with a remote call to an MVC controller function that can use my prebuilt functions to check for a valid address.

Now I did come up with a working solution to validate these fields but I desperately need to make it more dynamic so that in the future more textboxes can be added with minimal effort. Ideally I would like the logic to work something like validate on all inputs that start with ‘loc’.

Working solution (very dirty):

Simple form (in MVC view)

<form action="/Home/ViewResult" method="post" id="ViewResult" name="ViewResult">
<fieldset>
<legend>Enter Route</legend>
<p>
Address 1 (Start & End):
</p>
<p>
<input type="text" id="loc1" name="loc1" value='' />
</p>
<p>
Address 2:
</p>
<p>
<input type="text" id="loc2" name="loc2" value='' />
</p>
<p>
Address 3:
</p>
<p>
<input type="text" id="loc3" name="loc3" value='' />
</p>
<p>
Address 4:
</p>
<p>
<input type="text" id="loc4" name="loc4" value='' />
</p>
<p>
Address 5:
</p>
<p>
<input type="text" id="loc5" name="loc5" value='' />
</p>
<p>
<input type="submit" value="Route"/>
</p>
</fieldset>
</form>

jQuery validation code (in MVC view)

<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $("#ViewResult").validate({
            onfocusout: false,
            onkeyup: false, 
            rules: {
                "loc1": {
                    required: true,
                    remote: {
                        url: "/Home/IsValidAddress1",
                        timeout: 2000,
                        type: "post"
                    }
                },
                "loc2": {
                    required: true,
                    remote: {
                        url: "/Home/IsValidAddress2",
                        timeout: 2000,
                        type: "post"
                    }
                },
                "loc3": {
                    required: true,
                    remote: {
                        url: "/Home/IsValidAddress3",
                        timeout: 2000,
                        type: "post"
                    }
                },
                "loc4": {
                    remote: {
                        url: "/Home/IsValidAddress4",
                        timeout: 2000,
                        type: "post"
                    }
                },
                "loc5": {
                    remote: {
                        url: "/Home/IsValidAddress5",
                        timeout: 2000,
                        type: "post"
                    }
                }
            },
            messages: {
                "loc1": {
                    required: "Start/End Location is a required field.", 
                    remote: "Please enter a valid address."
                },
                "loc2": {
                    required: "Please enter at least 3 addresses.",
                    remote: "Please enter a valid address. "
                },
                "loc3": {
                    required: "Please enter at least 3 addresses.",
                    remote: "Please enter a valid address. "
                },
                "loc4": {
                    remote: "Please enter a valid address. "
                },
                "loc5": {
                    remote: "Please enter a valid address. "
                },
            } 

        });
    });

</script>

Functions in Home Controller referenced by remote


    // Function to check for a valid address
    public Boolean IsValidAddress(string location)
    {
        // If it is not blank
        if (location != "")
        {
            // Attempt to get the waypoint
            Waypoint waypoint = getWaypoint(location);

            // If no waypoint returned, return false
            if (waypoint == null)
            {
                return false;
            }
        }

        return true;
    }

    public JsonResult isValidAddress1(string loc1)  // Parameter must be textbox name
    {
        if (!IsValidAddress(loc1))
        {
            return new JsonResult { Data = false };
        }
        return new JsonResult { Data = true };
    }

    public JsonResult isValidAddress2(string loc2) // Parameter must be textbox name
    {
        if (!IsValidAddress(loc2))
        {
            return new JsonResult { Data = false };
        }
        return new JsonResult { Data = true };
    }

    public JsonResult isValidAddress3(string loc3) // Parameter must be textbox name
    {
        if (!IsValidAddress(loc3))
        {
            return new JsonResult { Data = false };
        }
        return new JsonResult { Data = true };
    }

    public JsonResult isValidAddress4(string loc4) // Parameter must be textbox name
    {
        if (!IsValidAddress(loc4))
        {
            return new JsonResult { Data = false };
        }
        return new JsonResult { Data = true };
    }

    public JsonResult isValidAddress5(string loc5) // Parameter must be textbox name
    {
        if (!IsValidAddress(loc5))
        {
            return new JsonResult { Data = false };
        }
        return new JsonResult { Data = true };
    }

PROBLEM:

Again this works but it is very dirty and is not at all dynamic.

Essentially I have two issues.

  1. How can I write the jQuery shorthand to create a validation rule for all textboxes starting with “loc”?
  2. As far as I can tell the MVC controller function that handles the remote call must have the name of the textbox passed to it. So how can I have one MVC controller function handle multiple remote calls to it?

I am not very strong in jQuery but what I would really want is something like this so I can add more textboxes later with minimal effort:

<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $("#ViewResult").validate({
            onfocusout: false,
            onkeyup: false, 
            rules: {
                "loc1": {
                    required: true,
                },
                "loc2": {
                    required: true,
                },
                "loc3": {
                    required: true,
                },
                $("input=text").StartsWith("loc").each(): {
                    remote: {
                        url: "/Home/IsValidAddress",
                        timeout: 2000,
                        type: "post"
                    }
                }
            },
            messages: {
                "loc1": {
                    required: "Start/End Location is a required field.", 
                },
                "loc2": {
                    required: "Please enter at least 3 addresses.",
                },
                "loc3": {
                    required: "Please enter at least 3 addresses.",
                },
                $("input=text").StartsWith("loc").each(): {
                    remote: "Please enter a valid address. "
                },
            } 

        });
    });

</script>

And the Home Controller functions


    // Function to check for a valid address
    public JsonResult IsValidAddress(string loc) // loc variable connect to dynamic textbox names?
    {
        // If it is not blank
        if (loc != "")
        {
            // Attempt to get the waypoint
            Waypoint waypoint = getWaypoint(loc);

            // If no waypoint returned, return false
            if (waypoint == null)
            {
                return new JsonResult { Data = false };
            }
        }

        return new JsonResult { Data = true };
    }

Finally note that I do not have the ability to change the MVC Model. I have seen many solutions similar to this that write the validation rules and remote calls directly in the MVC model, but I simply cannot do it that way.

Any suggestions for improvement are welcome and thanks in advance for any responses.

Please try and tell me where I went wrong or if what I want is even possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:31:05+00:00Added an answer on June 2, 2026 at 7:31 am

    So I figured it out.

    Final working solution:

    function in MVC Home Controller:

    
        // Function to check for a valid address
        // Note: address variable parameter connects to data attribute in remote call 
        public JsonResult IsValidAddress(string address) 
        {
            // If it is not blank
            if (address != "")
            {
                // Attempt to get the waypoint
                Waypoint waypoint = getWaypoint(address);
    
                // If no waypoint returned, return false
                if (waypoint.Location == null)
                {
                    return new JsonResult { Data = false };
                }
            }
            return new JsonResult { Data = true };
        }
    
    

    jQuery functions in View:

    <script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
    <script type="text/javascript">
    
    
        $(document).ready(function () {
            $("#ViewResult").validate({
                onfocusout: false,
                onkeyup: false
            });
            $("#loc1").rules("add", {
                required: true,
                messages: {
                    required: "Start/End Location is a required field.",
                }
            });
            $("#loc2").rules("add", {
                required: true,
                messages: {
                    required: "Please enter at least 3 addresses."
                }
            });
            $("#loc3").rules("add", {
                required: true,
                messages: {
                    required: "Please enter at least 3 addresses."
                }
            });
            $('#ViewResult [name^="loc"]').each(function () {
                var currentValue = null;
                currentValue = $(this);
                $(this).rules("add", {
                    remote: {
                        url: "/Home/IsValidAddress",
                        timeout: 2000,
                        type: "post",
                        data: { address: function () { return currentValue.val(); } }
                    },
                    messages: {
                        remote: "Please enter a valid address."
                    }
                });
            });
        });
    
    
    </script>
    

    I took so much time trying to figure this out that I thought I would share.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form that generates a new photo gallery, sending the title
I have simple form: <form action=add.php method=post > <input name=name maxlength=30/><br/> <textarea cols=80 name=description
I have a simple form created using Ajax.BeginForm : <% using (Ajax.BeginForm(Update, Description, new
Brief Description of the app: I have written a Delphi application that allows a
I have a subroutine that runs when I click a button on my form.
I have a simple feedback form that allows my users to log feedback and
I have a simple two-field form that stores its data in the database. For
I am using json-simple . If I have a JSON like: [ {id:1,name:...,description:...,dtStart:2012-03-27 03:00:00,dtEnd:2012-03-28
I have Man class which also has 2 variable : description and name. At
I have a simple table for reference page: id name description image In reference.php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.