8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

入力後に半角数字、ハイフンをチェックする

Last updated at Posted at 2016-04-05

電話番号に利用可能な文字列の入力のみサポートする入力欄を作りたくてサンプルを作ったのでメモ書き。

main.js
$( function() {
  $('#tel').on("keyup blur", function(){
    if( $(this).val().length > 0 ) {
        if(!$(this).val().match(/^[0-9\-]+$/)){
            alert("入力内容に半角数字、ハイフン以外が含まれています");
            return false;
        }
    } 
  })
} );
<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.19/jquery-ui.min.js"></script>
  <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.19/themes/redmond/jquery-ui.css">
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.19/i18n/jquery-ui-i18n.min.js"></script>
  <script src="script/main.js"></script>
  <style>
<!--
.ui-autocomplete {
    max-height: 100px;
    overflow-y: auto;
    overflow-x: hidden;
    padding-right: 20px;
}
# jquery-ui-autocomplete label {
    float: left;
    margin-right: 0.5em;
    color: black;
    font-size: 15px;
}
-->
</style>
</head>
<body>
  <div>
    <label> 電話番号:</label>
    <input type="text" id="tel" />
  </div>
</body>
</html>
8
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?