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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM