Check a string for certain characters

Possible Duplicate:
Checking if a string contains a certain substring

I use the jQuery .val() method to get the value of a certain input field.

How can I check if the returned string contains a certain character (for example, ',' )?

In my specific application, I use the code:

var tag_value = $("#tag_value").val();

to obtain comma-separated input values from the user, and I would like to check whether the string contains any commas.


You can just copy the contents of the jQuery string into a variable, and use indexOf on it as such:

var tag_value=$("#test").val();
value = tag_value;
if (value.indexOf(',') > -1) { alert('do something here') };

See example here

HTH

链接地址: http://www.djcxy.com/p/74098.html

上一篇: 检测div中的字符并将其删除Javascript或jQuery

下一篇: 检查某些字符的字符串