如何判断一个字符串在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代码,以检查用户输入的给定字符串不包含破折号,或者更好的是,仅包含字母数字字符?


#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" ); } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值