返回顶部

JS 判断输入字符串是否为数字、字母、下划线组成

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>RE</title>
</head>
<body>

    <p>
      使用正则表达式的方式来判断。
    </p>

<script>
function isValid(str) { return /^\w+$/.test(str); }
str = "1234abd__"
document.write(isValid(str));
document.write("<br>");

str2 = "$32343#"
document.write(isValid(str2));
document.write("<br>");
</script>

</body>
</html>

 

结果:

使用正则表达式的方式来判断。

true
false

posted @ 2020-03-29 21:38  Be-myself  阅读(2850)  评论(0编辑  收藏  举报
levels of contents 点击查看具体代码内容