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

猜你喜欢

转载自www.cnblogs.com/gengyufei/p/12595105.html