admin管理员组文章数量:1023628
I'm trying to create a nifty credit card number input field that automatically formats a credit card number as a user types it in. Here's what I have:
# Whenever the credit card input changes, update its value.
$(".credit-card-number").on "input", ->
# retrieve the credit card number
creditCardNumber = $(this).val()
# remove everything that's not a number
creditCardNumber = creditCardNumber.replace(/[^\d]+/g, "")
# ensure the value isn't more than 16 characters long
creditCardNumber = creditCardNumber[0..15]
# break apart the value every four numbers
creditCardNumber = creditCardNumber.match(/.{1,4}/g)
# insert spaces in the value when the value isn't null
creditCardNumber = if creditCardNumber? then creditCardNumber.join(" ") else ""
# set the value
$(this).val(creditCardNumber)
This works perfectly on the following HTML:
<input class="credit-card-number" id="credit_card_number" name="credit_card_number" placeholder="Credit card number" type="text">
However, if I set the input's type to number
, whenever I update the value and the value contains a space, Chrome changes the value to the empty string. Any ideas?
I'm trying to create a nifty credit card number input field that automatically formats a credit card number as a user types it in. Here's what I have:
# Whenever the credit card input changes, update its value.
$(".credit-card-number").on "input", ->
# retrieve the credit card number
creditCardNumber = $(this).val()
# remove everything that's not a number
creditCardNumber = creditCardNumber.replace(/[^\d]+/g, "")
# ensure the value isn't more than 16 characters long
creditCardNumber = creditCardNumber[0..15]
# break apart the value every four numbers
creditCardNumber = creditCardNumber.match(/.{1,4}/g)
# insert spaces in the value when the value isn't null
creditCardNumber = if creditCardNumber? then creditCardNumber.join(" ") else ""
# set the value
$(this).val(creditCardNumber)
This works perfectly on the following HTML:
<input class="credit-card-number" id="credit_card_number" name="credit_card_number" placeholder="Credit card number" type="text">
However, if I set the input's type to number
, whenever I update the value and the value contains a space, Chrome changes the value to the empty string. Any ideas?
-
4
Yes, just don't use a
number
input if you want space in the value. – mekwall Commented Jul 3, 2013 at 5:48 -
Why do you need to set the type to
number
as opposed totext
? – Jody Heavener Commented Jul 3, 2013 at 5:53 - 1 You can use regex to check your number format. I personally never trust the browsers about numbers, decimals because of localization issues – yakya Commented Jul 3, 2013 at 5:58
- 1 The reason I want to use a number input is because the value contained in the input is a number. It's the semantically correct input to use. Plus, on mobile devices it'll show a number keyboard instead of a full keyboard. – LandonSchropp Commented Jul 3, 2013 at 6:02
2 Answers
Reset to default 3Another thing you can do is use <input type="tel" />
instead of number
. I prefer to use that anyway for getting a numeric keyboard on mobile devices if you only want to input digits.
The formatting of the credit card number will work with tel
.
Scrap my previous answer.
The reason that you can't have the space is because jQuery is performing a validation of sorts on the value you are trying to set. Space is not allowed in a number, and so it's clearing the value. The exact same thing happens when you enter a letter.
There is nothing stopping you (at least in Chrome) from entering letters into a <input type="number">
field. It's just not valid and AFAIK if you submit the form, that field will contain an empty value (haven't tested this).
I'm trying to create a nifty credit card number input field that automatically formats a credit card number as a user types it in. Here's what I have:
# Whenever the credit card input changes, update its value.
$(".credit-card-number").on "input", ->
# retrieve the credit card number
creditCardNumber = $(this).val()
# remove everything that's not a number
creditCardNumber = creditCardNumber.replace(/[^\d]+/g, "")
# ensure the value isn't more than 16 characters long
creditCardNumber = creditCardNumber[0..15]
# break apart the value every four numbers
creditCardNumber = creditCardNumber.match(/.{1,4}/g)
# insert spaces in the value when the value isn't null
creditCardNumber = if creditCardNumber? then creditCardNumber.join(" ") else ""
# set the value
$(this).val(creditCardNumber)
This works perfectly on the following HTML:
<input class="credit-card-number" id="credit_card_number" name="credit_card_number" placeholder="Credit card number" type="text">
However, if I set the input's type to number
, whenever I update the value and the value contains a space, Chrome changes the value to the empty string. Any ideas?
I'm trying to create a nifty credit card number input field that automatically formats a credit card number as a user types it in. Here's what I have:
# Whenever the credit card input changes, update its value.
$(".credit-card-number").on "input", ->
# retrieve the credit card number
creditCardNumber = $(this).val()
# remove everything that's not a number
creditCardNumber = creditCardNumber.replace(/[^\d]+/g, "")
# ensure the value isn't more than 16 characters long
creditCardNumber = creditCardNumber[0..15]
# break apart the value every four numbers
creditCardNumber = creditCardNumber.match(/.{1,4}/g)
# insert spaces in the value when the value isn't null
creditCardNumber = if creditCardNumber? then creditCardNumber.join(" ") else ""
# set the value
$(this).val(creditCardNumber)
This works perfectly on the following HTML:
<input class="credit-card-number" id="credit_card_number" name="credit_card_number" placeholder="Credit card number" type="text">
However, if I set the input's type to number
, whenever I update the value and the value contains a space, Chrome changes the value to the empty string. Any ideas?
-
4
Yes, just don't use a
number
input if you want space in the value. – mekwall Commented Jul 3, 2013 at 5:48 -
Why do you need to set the type to
number
as opposed totext
? – Jody Heavener Commented Jul 3, 2013 at 5:53 - 1 You can use regex to check your number format. I personally never trust the browsers about numbers, decimals because of localization issues – yakya Commented Jul 3, 2013 at 5:58
- 1 The reason I want to use a number input is because the value contained in the input is a number. It's the semantically correct input to use. Plus, on mobile devices it'll show a number keyboard instead of a full keyboard. – LandonSchropp Commented Jul 3, 2013 at 6:02
2 Answers
Reset to default 3Another thing you can do is use <input type="tel" />
instead of number
. I prefer to use that anyway for getting a numeric keyboard on mobile devices if you only want to input digits.
The formatting of the credit card number will work with tel
.
Scrap my previous answer.
The reason that you can't have the space is because jQuery is performing a validation of sorts on the value you are trying to set. Space is not allowed in a number, and so it's clearing the value. The exact same thing happens when you enter a letter.
There is nothing stopping you (at least in Chrome) from entering letters into a <input type="number">
field. It's just not valid and AFAIK if you submit the form, that field will contain an empty value (haven't tested this).
本文标签: javascriptCredit card number formatStack Overflow
版权声明:本文标题:javascript - Credit card number format - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745508847a2153736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论