使用 jQuery 输入数字列表
我正在做一些工作,但仍然有 3 个问题。我尝试了很多方法,但没有什么用。如果您足够了解 jQuery,请建议我进行一些更改。
首先请查看以下网址:
http://jsfiddle.net/thisizmonster/maP57/
我在做什么是:
- 用户将输入电话号码。
- 数字长度仅为 8 个字符。
- 数字必须以 9 或 7 开头。表示 9xxxxxxx 或 7xxxxxxx。
- 用户最多可以输入 3 个数字。
- 数字必须用逗号分隔。
当前的问题是:
- 数字后面不能有逗号。仅在数字之间。
- 写入时验证 9xxxxxxx 或 7xxxxxxx 格式的数字。
- 他们无法输入连续逗号。我的意思是“,,”不允许。
如果您在 jsfiddle 上看不到示例,这里是复制版本:
HTML
<input type="text" id="sms-sender" />
JS
$(document).ready(function() {
$("#sms-sender").keypress(function(event) {
var numbers = $("#sms-sender").val().split(',');
var maxMsg = 3;
if (event.which != 44 && (event.which < 47 || event.which > 59) || numbers.length > maxMsg) {
event.preventDefault();
}
if (numbers.length > maxMsg) {
event.preventDefault();
}
if (event.which == 44) {
if (numbers.length <= maxMsg) {
$("#number-div").html("");
for (i = 0; i < numbers.length; i++) {
$("#number-div").append("<div class='numbers'> "+numbers[i]+"</div>").fadeIn('slow');
if (numbers[i].length != 8) {
$("#number-div").append("wrong")
}
}
}
}
if (numbers[numbers.length - 1].length > 7) {
if (event.which != 44) {
event.preventDefault();
}
}
});
});
I'm making some work, but there are 3 issues still I have. I tried many ways, but nothing useless. If you know jQuery enough, please suggest me some changes.
First please view below URL:
http://jsfiddle.net/thisizmonster/maP57/
What am I doing is:
- User will input phone number.
- Number will be only 8 char length.
- Number must begin with 9 or 7. Means 9xxxxxxx or 7xxxxxxx.
- User can enter maximum 3 numbers.
- Numbers must seperate with comma.
Current problem is:
- There mustn't any comma after numbers. Only between numbers.
- Validate numbers with 9xxxxxxx or 7xxxxxxx format while writing.
- They can't enter continues commas. I mean ",," don't allow.
If you can't see example on jsfiddle, here is copied version:
HTML
<input type="text" id="sms-sender" />
JS
$(document).ready(function() {
$("#sms-sender").keypress(function(event) {
var numbers = $("#sms-sender").val().split(',');
var maxMsg = 3;
if (event.which != 44 && (event.which < 47 || event.which > 59) || numbers.length > maxMsg) {
event.preventDefault();
}
if (numbers.length > maxMsg) {
event.preventDefault();
}
if (event.which == 44) {
if (numbers.length <= maxMsg) {
$("#number-div").html("");
for (i = 0; i < numbers.length; i++) {
$("#number-div").append("<div class='numbers'> "+numbers[i]+"</div>").fadeIn('slow');
if (numbers[i].length != 8) {
$("#number-div").append("wrong")
}
}
}
}
if (numbers[numbers.length - 1].length > 7) {
if (event.which != 44) {
event.preventDefault();
}
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用正则表达式来表示 9 或 7 (
[97]
) 后跟 7 个数字和逗号 (\d{7},
) 0 到 2 次 ({0,2}
) 涵盖您当前正在写入的数字之前的所有内容,而该数字又由 9 或 7 ([97]
) 后跟 0 到 7 位数字覆盖(\d{0,7}
) 全部可选 (?
)。http://jsfiddle.net/xTRph/
html:
JavaScript:
Using regular expressions to state, 9 or 7 (
[97]
) followed by 7 digits and a comma (\d{7},
) 0 to 2 times ({0,2}
) covers everything before the number you are currently writing which in turn is covered by 9 or 7 ([97]
) followed by 0 to 7 digits (\d{0,7}
) all optionally (?
).http://jsfiddle.net/xTRph/
html:
javascript:
2. 在输入 http://jsfiddle.net/2n7Tf/ 时使用 keyup 验证数字1/
1. & 2. 对于其他内容,您可以使用正则表达式。但我不确定你的逗号是什么意思。请解释一下。
如果数字与正则表达式匹配,则返回 true。所以你就可以了: http://jsfiddle.net/2n7Tf/4/
2. use keyup to validate numbers while typing http://jsfiddle.net/2n7Tf/1/
1. & 2. for the other stuff you can use regular expressions. But I am not sure what you mean with the comma. Please explain that.
will return true if number matches the regular expression. So here you go: http://jsfiddle.net/2n7Tf/4/