2
2

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.

フォーム入力有無バリデーション

2
Posted at

自分用メモ。
フォームの入力有無だけチェックしたいときに使い回せるかと。
基本的にtypenameだけ設定すれば動くようなイメージ。
他に必要な項目が増えたときに追記する。

こういうのはプラグイン化すれば良いんかなぁ(´・ω・`)

HTML

<form id="form" action="/">
	<table>
		<tr>
			<th>お名前</th>
			<td><input type="text" name="nameName"></td>
		</tr>
		<tr>
			<th>メールアドレス</th>
			<td><input type="email" name="nameEmail"></td>
		<tr>
		<tr>
			<th>ラジオボタン</th>
			<td>
				<input type="radio" name="nameRadio" value="1">
				<input type="radio" name="nameRadio" value="2">
				<input type="radio" name="nameRadio" value="3">
			</td>
		</tr>
		<tr>
			<th>チェックボックス</th>
			<td>
				<input type="checkbox" name="nameCheckbox" value="1">
				<input type="checkbox" name="nameCheckbox" value="2">
				<input type="checkbox" name="nameCheckbox" value="3">
			</td>
		</tr>
	</table>
	<button type="submit">送信</button>
</form>

JS

$(function() {
	var btn_enabled = function() {
		var $_form     = $('#form'),
			$_inputs   = $_form.find('input'),
			$_btn      = $_form.find('button[type="submit"]'),
			config_arr = [
				// お名前
				{
					'type' : 'text',
					'name' : 'nameName',
				},
				// メールアドレス
				{
					'type' : 'text',
					'name' : 'nameEmail',
				},
				// ラジオボタン
				{
					'type' : 'radio',
					'name' : 'nameRadio',
				},
				// チェックボックス
				{
					'type' : 'checkbox',
					'name' : 'nameCheckbox',
				}
			];

		// 読み込み時に発火
		change_btn_prop();

		// 値を変更したときに発火
		$_inputs.change(function() {
			change_btn_prop();
		});

		// チェック実行結果に応じてボタンのプロパティを変更
		function change_btn_prop() {
			execute_check(config_arr)
				? $_btn.attr('disabled', false)
				: $_btn.attr('disabled', true);
		}

		/* ****************************** */
		/* チェックの実行
		/* array = [
		/* 	{
		/* 		type    : input.attr('type'),
		/* 		name    : input.attr('name'),
		/* 	}
		/* ]
		/* ****************************** */
		function execute_check(array) {
			var check_arr = [],
				check_cntr = 0;
			for(var i=0; i<array.length; i++) {
				var result;
				switch(array[i]['type']) {
					case 'text':
						result = check_text(array[i]['name']);
						break;
					case 'radio':
						result = check_radio(array[i]['name']);
						break;
					case 'checkbox':
						result = check_checkbox(array[i]['name']);
						break;
				}
				check_arr.push(result);
			}
			return check_arr.filter(function(elm) { return elm === true }).length >= array.length ? true : false;
		}

		// テキストの入力チェック
		function check_text(name) {
			var $_input = $('input[name="'+name+'"]');
			return $_input.val() !== "" ? true :  false;
		}

		// ラジオボタンの入力チェック
		function check_radio(name) {
			var $_input = $('input[name="'+name+'"]'),
				result = false;
			$_input.each(function() {
				if($(this).attr('checked')) {
					result = true;
					return false;
				}
			});
			return result;
		}

		// チェックボタンの入力チェック
		function check_checkbox(name) {
			var $_input = $('input[name="'+name+'"]'),
				cntr = 0;
			$_input.each(function() {
				if($(this).attr('checked')) cntr++;
			});
			return cntr > 0 ? true : false;
		}
	}();

});

2
2
0

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?