如何判断字符串是否包含JavaScript中的某个字符? [英] How to tell if a string contains a certain character in JavaScript?
问题描述
我有一个带有文本框的页面,用户应输入24个字符(字母和数字,不区分大小写)注册码。我使用 maxlength
来限制用户输入24个字符。
I have a page with a textbox where a user is supposed to enter a 24 character (letters and numbers, case insensitive) registration code. I used maxlength
to limit the user to entering 24 characters.
注册码通常以字符组的形式给出用破折号分隔,但我希望用户输入没有破折号的代码。
The registration codes are typically given as groups of characters separated by dashes, but I would like for the user to enter the codes without the dashes.
如何在没有jQuery的情况下编写我的JavaScript代码来检查给定的字符串用户输入不包含破折号,或者更好的是,只包含字母数字字符?
How can I write my JavaScript code without jQuery to check that a given string that the user inputs does not contain dashes, or better yet, only contains alphanumeric characters?
推荐答案
在<$中查找hello c $ c> your_string
if (your_string.indexOf('hello') > -1)
{
alert("hello found inside your_string");
}
对于字母数字,您可以使用正则表达式:
For the alpha numeric you can use a regular expression:
http://www.regular-expressions.info/javascript .html
这篇关于如何判断字符串是否包含JavaScript中的某个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!