Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

javascript - 如何判断一个字符串在JavaScript中是否包含某个字符?(How to tell if a string contains a certain character in JavaScript?)

I have a page with a textbox where a user is supposed to enter a 24 character (letters and numbers, case insensitive) registration code.(我有一个带有文本框的页面,用户应在其中输入24个字符(字母和数字,不区分大小写)的注册码。)

I used maxlength to limit the user to entering 24 characters.(我使用maxlength将用户限制为输入24个字符。) 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.(注册代码通常以破折号分隔的字符组形式给出,但是我希望用户输入不带破折号的代码。) 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?(如何在没有jQuery的情况下编写我的JavaScript代码,以检查用户输入的给定字符串不包含破折号,或者更好的是,仅包含字母数字字符?)   ask by Daniel Allen Langdon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To find "hello" in your_string(在your_string查找“ hello”)

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(http://www.regular-expressions.info/javascript.html) Alpha Numeric Regular Expression(字母数字正则表达式)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...