match()で数字だけかチェック
- <!DOCTYPE html>
- <html lang="ja">
- <head>
- <meta charset="UTF-8">
- <title>match()で数字だけかチェック</title>
- <body>
- <h1>match()で数字だけかチェック</h1>
- <p>与えられた文字が数字(全角・半角不問)のみかどうかをチェックする。</p>
- <script type="text/javascript">
- function checkOnlyNumber(str)
- {
- if (str.match(/^[0-90-9]+$/) != null)
- {
- return true;
- }
- return false;
- }
- document.write("「dfghj」は");
- document.write(checkOnlyNumber("dfghj"));
- document.write("<br />");
- document.write("「654165」は");
- document.write(checkOnlyNumber("654165"));
- document.write("<br />");
- document.write("「65a4165」は");
- document.write(checkOnlyNumber("65a4165"));
- document.write("<br />");
- document.write("「45678」は");
- document.write(checkOnlyNumber("45678"));
- document.write("<br />");
- document.write("「454156678」は");
- document.write(checkOnlyNumber("454156678"));
- document.write("<br />");
- </script>
- <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
- <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
- <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
- </div>
- <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
- Copyright © 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
- </div>
- </body>
- </html>