Html 如何格式化信用卡输入字段和到期日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17017136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format credit card input fields and expiry date
提问by
Is there a way to code this with html/css?
有没有办法用 html/css 编码?
I want to format it so two digits either side of the "/" in the expiry box. I have just places the "/" in the form on my web page to show what I am trying to do.
我想将其格式化为到期框中“/”两侧的两位数字。我刚刚在我的网页上的表单中放置了“/”以显示我正在尝试做的事情。
Credit Card Number:
<input class="inputCard" type="text" name="creditCard1" id="creditCard1"/>
-
<input class="inputCard" type="text" name="creditCard2" id="creditCard2"/>
-
<input class="inputCard" type="text" name="creditCard3" id="creditCard3"/>
-
<input class="inputCard" type="text" name="creditCard4" id="creditCard4"/>
<br />
Card Expiry:
<input class="inputCard" type="text" name="expiry" id="expiry"/>
I want to format it so it will only accept four numbers in each credit card text box.
我想格式化它,以便它在每个信用卡文本框中只接受四个数字。
I don't need the code to validate my data with javascript (I know how to do this). I am struggling with the html design.
我不需要代码来使用 javascript 验证我的数据(我知道如何做到这一点)。我正在为 html 设计而苦苦挣扎。
采纳答案by DACrosby
I'd say do something like this:
我会说做这样的事情:
Card Expiration:
<select name='expireMM' id='expireMM'>
<option value=''>Month</option>
<option value='01'>January</option>
<option value='02'>February</option>
<option value='03'>March</option>
<option value='04'>April</option>
<option value='05'>May</option>
<option value='06'>June</option>
<option value='07'>July</option>
<option value='08'>August</option>
<option value='09'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
<select name='expireYY' id='expireYY'>
<option value=''>Year</option>
<option value='10'>2010</option>
<option value='11'>2011</option>
<option value='12'>2012</option>
</select>
<input class="inputCard" type="hidden" name="expiry" id="expiry" maxlength="4"/>
Then just add a piece of Javascript or jQuery to combine the values from expireMM
and expireYY
and set it as the value for expiry
. I'd prefer doing it all server-side in PHP, but that depends on your exact setup. Either way, this is a common way to handle expiration dates.
然后只需添加一段 Javascript 或 jQuery 来组合来自expireMM
和的值expireYY
并将其设置为expiry
. 我更喜欢在 PHP 中完成所有服务器端的操作,但这取决于您的确切设置。无论哪种方式,这是处理到期日期的常用方法。
回答by wakqasahmed
Following code will work for you, however it is not a good practice as html5 is not fully supported by each and every browser in different devices. (better to use plugin like http://digitalbush.com/projects/masked-input-plugin/)
以下代码对您有用,但这不是一个好习惯,因为不同设备上的每个浏览器都不完全支持 html5。(最好使用像http://digitalbush.com/projects/masked-input-plugin/这样的插件)
Credit Card Number:
<input class="inputCard" type="number" min="1000" max="9999" name="creditCard1" id="creditCard1" required/>
-
<input class="inputCard" type="number" min="1000" max="9999" name="creditCard2" id="creditCard2" required/>
-
<input class="inputCard" type="number" min="1000" max="9999" name="creditCard3" id="creditCard3" required/>
-
<input class="inputCard" type="number" min="1000" max="9999" name="creditCard4" id="creditCard4" required/>
<br />
Card Expiry:
<input class="inputCard" name="expiry" id="expiry" type="month" required/>