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