jquery验证用逗号输入的文本 [英] jquery validating text entered with comma
本文介绍了jquery验证用逗号输入的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下内容来检查输入的文字是否有逗号。
I have the below to check if the text entered has a comma.
$("#keywords").change(function() {
if (this.value.indexOf(",") == -1) {
alert('Please separate multiple keywords with a comma.');
}
});
我基本上想要一种方法来告诉用户他是否输入了多个没有逗号的单词,他需要用逗号分隔它。上述变化事件似乎每次都会触发。我相信有一个更好的事件要做到这一点。任何人都可以建议。谢谢
I basically want a way to tell the user if he is entering more than one word without a comma, he needs to separate it with a comma. The above change event seems to trigger everytime. I am sure there is a better event to do this. Can any one please suggest. thanks
推荐答案
您可以在用户提交数据时验证输入:
You could just validate the input when the user submits the data:
$('#warningMessage').hide()
$('#submit').click(function(e) {
var isValid = true;
$('#value').each(function() {
if (($.trim($(this).val()).indexOf(",") == -1)) {
//alert('Please separate multiple keywords with a comma.');
$('#warningMessage').show();
} else {
$('#warningMessage').hide()
}
});
if (isValid == false) e.preventDefault();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="label">Value</div>
<input type="text" id="value" name="value" />
<br />
<div style="margin-left:140px;">
<input type="submit" name="submit" value="Submit" id="submit" />
</div>
<div style="color:orange;" id="warningMessage"><b>Please separate multiple keywords with a comma</b>
</div>
这篇关于jquery验证用逗号输入的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文