我正在尝试验证在文本区域输入的值,用户是否在逗号分隔值中输入了有效的十位数字,如
9876543210,98766543211,98766543212
如果用户输入的数字超过或少于10位,则必须提醒某些错误。
到目前为止,我尝试了类似
的内容$("#myMso").keyup(function(){
var value = $(this).val().replace(" ", "");
var word = value.split(",");
var lengths = words.map(function(word) {
if(lengths != 10){
alert("Hey! some of the contact number you have entered is not valid");
}
});
});
答案 0 :(得分:1)
试试这个
$("#myMso").keyup(function(){
var value = $(this).val();
var word = value.split(",");
$.each(word,function (key,value) {
if(value.length != 10){
alert("Hey! some of the contact number you have entered is not valid");
}
});
});
更改后事件的修改: -
$("#myMso").on('change',function(){
var value = $(this).val();
var word = value.split(",");
$.each(word,function (key,value) {
if(value.length != 10){
alert("Hey! some of the contact number you have entered is not valid");
}
});
});
答案 1 :(得分:1)
你滥用了地图功能。
map()方法创建一个新数组,其中包含调用a的结果 为调用数组中的每个元素提供了函数。
应该是这样的:
var lengths = words.map(function(word) {
// map should return your mapping value.
return word.length;
})
if (lengths.length > 0) {
alert("Hey! " + lengths.length + " contact numbers are wrong")
}
答案 2 :(得分:0)
您可以使用正则表达式搜索文本: -
<强> / ^ \ d {10}(\ d {10})* $ /克强>
答案 3 :(得分:0)
希望此代码适合您
$("#myMso").change(function(){
var value = $(this).val();
checkNumbers(value)
});
function checkNumbers (d) {
let numbers = d.split(',');
numbers.forEach((number, i) => {
number.length < 10 && alert('Incorrect Number' + number);
});
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;