欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何判断一个字符串在JavaScript中是否包含某个字符?

程序员文章站 2023-12-21 16:59:04
...

本文翻译自: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代码,以检查用户输入的给定字符串不包含破折号,或者更好的是,仅包含字母数字字符?


#1楼

参考:https://stackoom.com/question/IeD7/如何判断一个字符串在JavaScript中是否包含某个字符


#2楼

检查字符串(单词/句子...)是否包含特定的单词/字符

if ( "write something here".indexOf("write som") > -1 )  { alert( "found it" );  } 

#3楼

Working perfectly.This exmple will help alot. 完美的工作。这个例子会很有帮助。

<script>    
    function check()
    {
       var val = frm1.uname.value;
       //alert(val);
       if (val.indexOf("@") > 0)
       {
          alert ("email");
          document.getElementById('isEmail1').value = true;
          //alert( document.getElementById('isEmail1').value);
       }else {
          alert("usernam");
          document.getElementById('isEmail1').value = false;
          //alert( document.getElementById('isEmail1').value);
       }
    }
</script>

<body>
    <h1>My form </h1>
    <form action="v1.0/user/login" method="post" id = "frm1">
        <p>
            UserName : <input type="text" id = "uname" name="username" />
        </p>
        <p>
            Password : <input type="text" name="password" />
        </p>
        <p>
            <input type="hidden" class="email" id = "isEmail1" name = "isEmail"/>
        </p>
        <input type="submit" id = "submit" value="Add User" onclick="return check();"/>
    </form>
</body>

#4楼

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: http://jsfiddle.net/oliverni/cb8xw/ 这是一个示例: http : //jsfiddle.net/oliverni/cb8xw/


#5楼

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 find

string1.test(regex); // => true
string2.test(regex); // => false

Learn Regex in 5 minutes? 在5分钟内学习正则表达式?


#6楼

 var inputString = "this is home"; var findme = "home"; if ( inputString.indexOf(findme) > -1 ) { alert( "found it" ); } else { alert( "not found" ); } 

相关标签: javascript string

上一篇:

下一篇: