PaymentCurrencyAmount.value - Web APIs 编辑

Secure context

This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The PaymentCurrencyAmount property value is a string containing the decimal numeric value of the payment, specified in the currency units indicated by the currency property. The contents of this string must be a valid decimal number; that is, some number of digits between 0 and 9 with up to one optional decimal point. An optional leading minus sign ("-") can be included to indicate a negative value, such as to represent a refund or discount.

Important note: The number given in this string is always specified using the period (".") as the decimal point, rather than the comma (","), even if the user's locale normally uses the comma. You must convert the entered text to this form or it will not be valid.

Syntax

value = paymentCurrencyAmount.value;

Value

A DOMString indicating the numeric value of the payment. This must be a valid decimal number, with an optional leading minus sign ("-"), then one or more decimal digits 0 through 9, optionally with a decimal point (".") with at least one digit following it to represent fractional units. There must not be any leading or trailing whitespace in the string.

For uniformity and consistency, the value is always given using the period (".") as the decimal character, regardless of the user's locale. You need to convert the value to this format before submitting the payment.

See the example Verifying a properly formatted price below for a simple regular expression that can be used to validate the value string prior to submission.

Examples

Representing prices

This example represents the price of $42.95 in US dollars:

let itemPrice = {
  currency: "USD",
  value: "42.95"
};

This example specifies a price of £7.77:

let shippingCost = {
  currency: "GBP",
  value: "7.77"
}

This example specifies a price of 1000¥:

let price = {
  currency: "JPY",
  value: "1000"
}

Verifying a properly formatted price

You can ensure that the value entered as a price is formatted correctly prior to submission by matching it against a simple regular expression:

function checkPriceFormat(price) {
  let validRegex = /^-?[0-9]+(\.[0-9]+)?$/;

  return validRegex.test(price);
}

This function, checkPriceFormat(), will return true if the specified price string is formatted properly, or false if it's not.

Specifications

SpecificationStatusComment
Payment Request API
The definition of 'PaymentCurrencyAmount.value' in that specification.
Candidate Recommendation

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:61 次

字数:5083

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文