使用jQuery查看输入是否包含空格 [英] See if Input Has Spaces with jQuery
本文介绍了使用jQuery查看输入是否包含空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个读取输入值的函数,并确定是否在其创建中使用了空格。我不打算修剪任何东西 - 看看它是否需要修剪能力。
I'm trying to write a function that reads a inputs value, and determines if a space has been used in it's creation. I'm not trying to trim anything -- just see if it would have needed the ability to trim.
我甚至不知道从哪里开始,所以我没有任何代码可供查看。如果可以,请帮忙!
I'm not even sure where to begin on this, so I don't have any code to look at. Please help if you can!
我正在尝试这个解决方案,但它似乎不起作用(我不认为我正在使用.has())
I was trying this solution but it doesn't seem to work (I don't think I'm using .has() correctly)
if ($('#id').val().has(" ")) {
alert('has spaces')
}
推荐答案
var hasSpace = $('#myInputId').val().indexOf(' ')>=0;
如果你想测试其他类型的空格(例如制表),你可以使用正则表达式:
If you want to test other types of "spaces" (for example a tabulation), you might use a regex :
var hasSpace = /\s/g.test($('#myInputId').val());
这篇关于使用jQuery查看输入是否包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文