本文共 1819 字,大约阅读时间需要 6 分钟。
本文翻译自:
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代码,以检查用户输入的给定字符串不包含破折号,或者更好的是,仅包含字母数字字符?
参考:
检查字符串(单词/句子...)是否包含特定的单词/字符
if ( "write something here".indexOf("write som") > -1 ) { alert( "found it" ); }
Working perfectly.This exmple will help alot. 完美的工作。这个例子会很有帮助。
My form
Try this: 尝试这个:
if ('Hello, World!'.indexOf('orl') !== -1) alert("The string 'Hello World' contains the substring 'orl'!");else alert("The string 'Hello World' does not contain the substring 'orl'!");
Here is an example: 这是一个示例: :
You're all thinking too hard. 你们都在想 Just use a simple Regular Expression, it's your best friend. 只需使用一个简单的正则表达式,它就是您最好的朋友。
var string1 = "Hi Stack Overflow. I like to eat pizza."var string2 = "Damn, I fail."var regex = /(pizza)/g // Insert whatever phrase or character you want to findstring1.test(regex); // => truestring2.test(regex); // => false
var inputString = "this is home"; var findme = "home"; if ( inputString.indexOf(findme) > -1 ) { alert( "found it" ); } else { alert( "not found" ); }
转载地址:http://mgcnb.baihongyu.com/