我有一个<textarea>
需要验证双引号之间的所有内容是纬度还是经度的地方。我已经设置好了,这个词不能使用超过 3 次,这很好。但是我有一条错误消息显示何时使用了错误的格式。当用户专注并且没有在引号之间放置任何内容或者它不是纬度或经度时,我需要弹出错误。
这是我的演示:
$('#test').on('keydown focusout', function(e){
var word = 'latitude',
count = this.value.match(new RegExp('"\\b'+word+'\\b"','g')) || [],
limiter = $('#output');
$('#output').text(count.length);
return !(count.length > 2 && e.which != 8);
});
//Error - Max limit reached
$('#test').bind('keyup focusout', function(){
limiter = $('#output');
if(limiter.text() == '3'){
$('#limitReached').attr("class","hi");
$('#limitReached').text("You cannot exeed more than 10 coorniates");
$('#test').css({'border': '1px solid red'});
}
else{
$('#limitReached').attr("class","bye");
$('#limitReached').text("");
$('#test').css({'border': '1px solid black'});
}
});
//Error - Format is wrong
$('#test').on('focusout', function(e){
var word1 = 'latitude',
word2 = 'longitude',
count = this.value.match(new RegExp('"\\b'+word1+'\\b"','g')) || [];
if ($(this).val() != count){
$('#limitReached').attr("class","hi");
$('#limitReached').html('Please use correct JSON format:<br> example - [{"latitude":33.851871,"longitude":-84.364336},]');
$('#test').css({'border': '1px solid red'});
}
else{
$('#limitReached').attr("class","bye");
$('#limitReached').text("");
$('#test').css({'border': '1px solid black'});
}
});