1

I am trying to substitute parameters in my url using jquery with my input in my textbox. Also, I need to percent-encode the input. I am trying to use jquery.validator.format however it is not working. Suggestions?

<script src="http://code.jquery.com/jquery-latest.js"></script>
  <script src="http://jqueryjs.googlecode.com/svn/trunk/plugins/validate/jquery.validate.js"></script>
<script language="javascript" type="text/javascript">    
$(document).ready(function () {
        $("button").click(function () {
            var str = "http://api.geonames.org/postalCodeSearchJSON?placename={0}&maxRows=10&username=<my username>";
            alert(str);
            var loc = $("#location").val();
            alert(loc);
            str = jQuery.validator.format(str, loc);
            alert(str);
        });
    });
3
  • Where do the substitution strings (for placename and username) come from? Commented Mar 9, 2011 at 3:34
  • They come from the textbox #location and username is hardcoded i am just not displaying it for security reasons Commented Mar 9, 2011 at 3:38
  • Please post your html code so we can relate on what you are doing. :) Commented Mar 9, 2011 at 3:40

2 Answers 2

2

This should work just fine, including percent encoding (via encodeURIComponent()):

$("button").click(function () {
    var str = "http://api.geonames.org/postalCodeSearchJSON?placename={0}&maxRows=10&username=redacted";
    var loc = encodeURIComponent($("#location").val());
    str = jQuery.validator.format(str, loc);
    alert(str);
});

jsFiddle demo: http://jsfiddle.net/mattball/FFVCj/

Sign up to request clarification or add additional context in comments.

Comments

0
 $(document).ready(function () {
    $("button").click(function () {
     //   var str = "http://api.geonames.org/postalCodeSearchJSON?placename={0}&maxRows=10&username=rbur0425";
    //    alert(str);
        var loc = $("#location").val();
  //      alert(loc);
        //    var loc2 = encodeURIComponent(loc);
        var url = "http://api.geonames.org/postalCodeSearchJSON?placename=" + loc + "&maxRows=10&username=rbur0425";
 //       alert(encodeURI(url));
    });
});

works good for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.