From jQuery JavaScript Library
« Back to Plugins/Validation
jQuery.validator.format( template, [argument], [argumentN...] )
Replaces {n} placeholders with arguments.
One or more arguments can be passed, in addition to the string template itself, to insert into the string.
Arguments:| template | String | |
|---|
| The string to format. |
| argument (Optional) | String, Array<String> | |
|---|
| The first argument to insert, or an array of Strings to insert |
| argumentN... (Optional) | String | |
|---|
| The second etc. argument to insert |
Examples:| Name | Type |
Demonstrates basic usage.
$("button").click(function () {
var str = "Hello {0}, this is {1}";
alert("'" + str + "'");
str = jQuery.validator.format(str, "World", "Bob");
alert("'" + str + "'");
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script>
$(document).ready(function(){
$("button").click(function () {
var str = "Hello {0}, this is {1}";
alert("'" + str + "'");
str = jQuery.validator.format(str, "World", "Bob");
alert("'" + str + "'");
});
});
</script>
</head>
<body>
<button>Show format Example</button>
</body>
</html>
Shows how to first create a template function by passing only the string argument and then calling it later.
$("button").click(function () {
var template = jQuery.validator.format("Please enter more {0}, or order {0} by mail (see {1})");
alert(template("Apples", "our FAQ"));
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script>
$(document).ready(function(){
$("button").click(function () {
var template = jQuery.validator.format("Please enter more {0}, or order {0} by mail (see {1})");
alert(template("Apples", "our FAQ"));
});
});
</script>
</head>
<body>
<button>Show format Example</button>
</body>
</html>