// 入力調整 // // // ▼数値型 // # ^[0-9]+$ // # ^(-)?[0-9]+$ [auto:-adjust-numeric] // # ^[0-9]+(\.[0-9]+)$ // # ^(-)?[0-9]+(\.[0-9]+)?$ // // ▽3桁区切り「-adjust-locale」 // # ^[0-9]+$ ⇒ 3桁区切り [auto:-adjust-numeric] // # ^[0-9]+(\.[0-9]+)?$ ⇒ 3桁区切り // ※初期変換します(でも可) // 123456123,456 // -123456-123,456 // 123,4,56.2345123,456.2345 // // ▼文字型 // ※-adjust-minus、-adjust-float、-adjust-localeは無効 // # ^[0-9]+$ // # ^[0-9\-]+$ // # ^[a-z]+$ (自動変換:大文字⇒小文字) // # ^[A-Z]+$ (自動変換:小文字⇒大文字) // # ^[A-Za-z]+$ // # ^[A-Za-z0-9]+$ // # ^[A-Z0-9]+$(a-z⇒A-Z) // # permit[...] ex⇒ #.<>] // // ▼数値型制限 [文字型以外auto:-adjust-numeric -adjust-minus] // # tinyint型 :-128~127 // # smallint型 :-32768~32767 // # mediumint型 :-8388608~8388607 // # int型 :-2147483648~2147483647 // # bigint型 :-9223372036854775808~9223372036854775807 // ▽符号なし整数型制限 [文字型以外auto:-adjust-numeric] // # tinyint型(unsigned) :0~255 // # smallint型(unsigned) :0~65535 // # mediumint型(unsigned):0~16777215 // # int型(unsigned) :0~4294967295 // # bigint型(unsigned) :0~18446744073709551615 // ▽最大値、最小値 [文字型以外auto:-adjust-numeric]:マイナス設定可、floatも対応(最小値が最大値を超える場合は、最小値と最大値を入れ替えます) // # 最大値:~200 // # 最小値:-10.5~ // // ▼桁数制限 // # 数字:~999999 (マイナス記号は無視)[文字型以外auto:-adjust-numeric] // # 実数:~999.999(マイナス記号は無視)[auto:-adjust-float] // # 文字:5文字 // # 文字:6文字 // // ▼よみがな入力補助 // # カタカナ // # 半角カタカナ // # ひらがな // # classでの指定方法 // # 対象が複数 // // ▼最終日補正 // 日付に「-adjust-day」:「name="nyusha_day"」の「day」が「year」「month」のエレメントで最終日補正 // // // $(function() { adjustObj = new adjustClass(); }); function adjustClass() { var this_ = this; $('[class*=-adjust]').each(function(){ var className = ' '+$(this).attr('class')+' '; if( className.match( / -adjust-[^ ]+ / ) ) { var parts; // ルビ if( parts = className.match( / -adjust-ruby(-(\w))?\[([^ ]+) / ) ) { if( parts[2] ) { parts[3] = parts[3].replace( /\]$/, '' ).replace(/([\[\]])/g,'\\$1'); this_.setRubyList( this, parts[3] ); var ele = this; $.each( Object.keys( ele.rubyList ), function(idx,ruby){ var rubyEle = $('.-adjust-ruby\\['+ruby+'\\]'); if( $(rubyEle).length == 0 ) { rubyEle = $(ele).closest('form').find('[name='+ruby+']'); if( $(rubyEle).length == 0 ) return true; } if( $(rubyEle).eq(0).data('-adjust-ruby') != undefined ) return true; var state = {'name':ruby,'base':$(rubyEle).eq(0).val(),'kanaEle':ele,'type':parts[2],'pattern':{}}; state['pattern'][$(rubyEle).eq(0).val()] = ele.rubyList[ruby]; $(rubyEle).eq(0).addClass('-adjust-ruby-set').data('-adjust-ruby',state); }); switch( parts[2] ) { case 'k': $(this).change( function() { $(this).val( this_.zen2han( $(this).val() ) ); this_.setRubyList( this ); } ); break; case 'H': $(this).change( function() { $(this).val( this_.kata2hira( $(this).val() ) ); this_.setRubyList( this ); } ); break; default: $(this).change( function() { $(this).val( this_.hira2kata( $(this).val() ) ); this_.setRubyList( this ); } ); } } // 最終日 } else if( $(this).hasClass('-adjust-day') ) { var yearEle = $('[name='+$(this).attr('name').replace( /day/, 'year' ).replace( /(\[|\])/g, '\\$1' )+']'); var monthEle = $('[name='+$(this).attr('name').replace( /day/, 'month' ).replace( /(\[|\])/g, '\\$1' )+']'); if( yearEle.length && monthEle.length ) { var dayEle = $(this); yearEle.change(function(){ this_.setDay( yearEle, monthEle, dayEle ); }); monthEle.change(function(){ this_.setDay( yearEle, monthEle, dayEle ); }); this_.setDay( yearEle, monthEle, dayEle ); } } else { // 半角 if( $(this).hasClass('-adjust-locale') ) { switch( $(this).prop('tagName') ) { case 'INPUT': case 'SELECT': case 'TEXTAREA': $(this).val( this_.comma( $(this).val().replace( /,/g, '' ) ) ); break; default: $(this).text( this_.comma( $(this).text().replace( /,/g, '' ) ) ); } } // 最大値、最小値 if( parts = className.match( / -adjust-(min|max)\[([^ ]+) / ) ) { $(this).blur( function(e) { if( !$(this).data('release') ) this_.up( e, this, true ); } ); } $(this).keyup(function(e){ this_.up( e, this ); }); $(this).mouseup(function(e){ this_.up( e, this ); }); } } }); // ルビ監視 if( $('.-adjust-ruby-set').length ) setInterval( ( function( this_ ) { return function() { this_.ruby(); } } )( this ) ,30 ); } // ルビ adjustClass.prototype.setRubyList = function( ele, ruby ) { var rubyList = ( ruby !== undefined )? ruby.split( /,/ ): Object.keys( ele.rubyList ); var valList = $(ele).val().split( /[  ]/ ); var firstVal = valList.length - rubyList.length; if( firstVal < 0 ) firstVal = 0; ele.rubyList = []; $.each(rubyList,function(idx,ruby){ ele.rubyList[ruby] = ( !idx && firstVal > 0 )? valList.slice( 0, firstVal+1 ).join(' '): ( valList[idx+firstVal] ?? '' ); }); } // 最終日 adjustClass.prototype.setDay = function( yearEle, monthEle, dayEle ) { if( yearEle.val() && monthEle.val() ) { var day = ( new Date( yearEle.val(), monthEle.val(), 0 ) ).getDate(); if( dayEle.val() > day ) dayEle.val( [day] ) dayEle.find('option').each(function(){ $(this).css( 'display', ( !$(this).val() || $(this).val() <= day )? '': 'none' ); }); } } adjustClass.prototype.up = function( e, ele, blurCheck ) { var op = Array(); var className = ' '+$(ele).attr('class')+' '; var parts; var backspace = ( e.key == 'Backspace' || e.key == 'Delete' ); // カーソル位置 var cursor = $(ele).get(0).selectionStart; // 漢字入力強制解除 if( !blurCheck ) { $(ele).data('release',true); $(ele).blur(); $(ele).data('release',false); $(ele).focus(); } var val = $(ele).val(); // オプション if( $(ele).hasClass('-adjust-alphabet') ) op['2alphabet'] = true; if( $(ele).hasClass('-adjust-ALPHABET') ) op['2ALPHABET'] = true; if( $(ele).hasClass('-adjust-alphabet') || $(ele).hasClass('-adjust-Alphabet') ) op['alphabet'] = true; if( $(ele).hasClass('-adjust-ALPHABET') || $(ele).hasClass('-adjust-Alphabet') ) op['ALPHABET'] = true; if( $(ele).hasClass('-adjust-tel') ) op['tel'] = true; if( $(ele).hasClass('-adjust-number') || op['tel'] ) op['number'] = true; if( parts = className.match( / -adjust-permit\[([^\s]+) / ) ) op['permit'] = new RegExp( "["+parts[1].replace( /\]/, '' ).replace( /(.)/g, '\\$1' )+"]" ); if( Object.keys( op ).length == 0 ) { op['numeric'] = true; op['number'] = true; op['float'] = $(ele).hasClass('-adjust-float'); op['locale'] = $(ele).hasClass('-adjust-locale'); if( $(ele).hasClass('-adjust-tinyint') ) { op['min'] = '-128'; op['max'] = '127'; } if( $(ele).hasClass('-adjust-smallint') ) { op['min'] = '-32768'; op['max'] = '32767'; } if( $(ele).hasClass('-adjust-mediumint') ) { op['min'] = '-8388608'; op['max'] = '8388607'; } if( $(ele).hasClass('-adjust-int') ) { op['min'] = '-2147483648'; op['max'] = '2147483647'; } if( $(ele).hasClass('-adjust-bigint') ) { op['min'] = '-9223372036854775808'; op['max'] = '9223372036854775807'; } if( $(ele).hasClass('-adjust-tinyint_unsigned') ) { op['max'] = '255'; } if( $(ele).hasClass('-adjust-smallint_unsigned') ) { op['max'] = '65535'; } if( $(ele).hasClass('-adjust-mediumint_unsigned') ) { op['max'] = '16777215'; } if( $(ele).hasClass('-adjust-int_unsigned') ) { op['max'] = '4294967295'; } if( $(ele).hasClass('-adjust-bigint_unsigned') ) { op['max'] = '18446744073709551615'; } if( parts = className.match( / -adjust-min\[([\d\-\.]+)/ ) ) op['min'] = parts[1]; if( parts = className.match( / -adjust-max\[([\d\-\.]+)/ ) ) op['max'] = parts[1]; if( 'min' in op ) { var s = op['min'].split('.'); if( s.length > 1 ) op['float'] = true; op['min-int'] = s[0]; op['min-decimal'] = s[1] ?? '0'; op['min-minus'] = op['min'].match( /^-/ ) ? true: false; if( op['min-minus'] ) op['minus'] = true; } if( 'max' in op ) { var s = op['max'].split('.'); if( s.length > 1 ) op['float'] = true; op['max-int'] = s[0]; op['max-decimal'] = s[1] ?? '0'; op['max-minus'] = op['max'].match( /^-/ ) ? true: false; if( op['max-minus'] ) op['minus'] = true; } if( 'min' in op && 'max' in op ) { var swap = false; if( !op['min-minus'] && op['max-minus'] ) { swap = true; } else if( op['min-minus'] == op['max-minus'] ) { if( op['min-int'].length == op['max-int'].length ) { if( op['min-int'] == op['max-int'] ) { swap = ( op['min-decimal'] < op['max-decimal'] )? op['min-minus']: !op['min-minus']; } else { swap = ( op['min-int'] < op['max-int'] )? op['min-minus']: !op['min-minus']; } } else { swap = ( op['min-int'].length < op['max-int'].length )? op['min-minus']: !op['min-minus']; } } if( swap ) { [ op['min'], op['max'] ] = [ op['max'], op['min'] ]; [ op['min-int'], op['max-int'] ] = [ op['max-int'], op['min-int'] ]; [ op['min-decimal'], op['max-decimal'] ] = [ op['max-decimal'], op['min-decimal'] ]; [ op['min-minus'], op['max-minus'] ] = [ op['max-minus'], op['min-minus'] ]; } } if( 'min' in op || 'max' in op ) { op['minus'] = ( op['min-minus'] || op['max-minus'] ); if( $(ele).hasClass('-adjust-minus') ) op['minus'] = !op['minus']; } else { op['minus'] = $(ele).hasClass('-adjust-minus'); } } if( parts = className.match( / -adjust-length\[(\d+)(,(\d+))?\] / ) ) { if( op['numeric'] ) { op['length'] = Number( parts[1] ); if( op['float'] || parts[3] ) { op['float'] = true; op['decimal'] = Number( parts[3] ); op['length'] -= op['decimal']; if( op['length'] < 1 ) op['length'] = 0; } } else { // 桁数制限:文字 if( val.length > parts[1] ) { var end = cursor -1; if( end > parts[1] ) end = parts[1]; var newVal = val.slice( 0, end ); var last = cursor - Number( parts[1] ) -1; if( last < 0 ) newVal += val.slice( last ) $(ele).val( newVal ); $(ele).get(0).setSelectionRange( cursor -1, cursor -1 ); return true; } } } // 全角⇒半角、大文字・小文字 val = val.replace( /。/g, '.' ).replace( /ー/g, '-' ).replace( /[!-~]/g, function(s){ return String.fromCharCode(s.charCodeAt(0)-0xFEE0); } ); if( op['2alphabet'] ) val = val.toLowerCase( val ); if( op['2ALPHABET'] ) val = val.toUpperCase( val ); // 不要な文字を削除 var newVal = ''; var dotCount = ( val.match( /\./g ) || [] ).length; var dotSet = false; var invalid = 0; for( var i = 0; i < val.length; i++ ) { var c = val.charAt(i); if( ( op['number'] && c.match( /[0-9]/ ) ) // 数字 || ( op['alphabet'] && c.match( /[a-z]/ ) ) // 英字(小文字) || ( op['ALPHABET'] && c.match( /[A-Z]/ ) ) // 英字(大文字) || ( op['permit'] && c.match( op['permit'] ) ) // 追加指定 || ( op['minus'] && c == '-' && !i ) // マイナス記号(先頭のみ) || ( op['tel'] && c == '-' ) // 電話 ) { newVal += c; } else if( op['float'] && c == '.' && !dotSet && ( dotCount == 1 || i != cursor -1 ) ) { // ドット newVal += c; dotSet = true; } else if( i < cursor ) { invalid++; } } cursor -= invalid; // 数値制限 if( op['numeric'] && newVal != '' ) { // 桁数 if( op['length'] ) { var s = newVal.split('.'); var minus = s[0].match( /^-/ ); var num = s[0].replace( /^-/, '' ); if( num.length > op['length'] ) { cursor -= num.length - op['length']; newVal = '9'.repeat( op['length'] ); if( minus ) newVal = '-'+newVal; if( op['decimal'] ) newVal += '.'+'9'.repeat( op['decimal'] ); } else if( op['float'] && s.length > 1 ) { if( !op['decimal'] ) { newVal = s[0]; } else if( s[1].length > op['decimal'] ) { s[1] = s[1].slice( 0, op['decimal'] ); newVal = s[0]+'.'+s[1]; } } } // 最大値、最小値(桁数が大きいので文字で比較) if( !backspace ) { if( op['min'] ) { var strVal = String( newVal ); var s = strVal.split('.'); var strInt = s[0]; var strDecimal = s[1] ?? '0'; var strMinus = strVal.match( /^-/ ) ? true: false; var swap = false; if( !op['min-minus'] && strMinus ) { swap = true; } else if( op['min-minus'] == strMinus && ( op['min-minus'] || blurCheck ) ) { if( op['min-int'].length == strInt.length ) { if( op['min-int'] == strInt ) { swap = ( op['min-decimal'] < strDecimal )? strMinus: !strMinus; } else { swap = ( op['min-int'] < strInt )? strMinus: !strMinus; } } else { swap = ( op['min-int'].length < strInt.length )? strMinus: !strMinus; } } if( swap ) newVal = op['min']; } if( op['max'] ) { var strVal = String( newVal ); var s = strVal.split('.'); var strInt = s[0]; var strDecimal = s[1] ?? '0'; var strMinus = strVal.match( /^-/ ) ? true: false; var swap = false; if( !strMinus && op['max-minus'] ) { swap = true; } else if( strMinus == op['max-minus'] && ( !op['max-minus'] || blurCheck ) ) { if( strInt.length == op['max-int'].length ) { if( strInt == op['max-int'] ) { swap = ( strDecimal < op['max-decimal'] )? strMinus: !strMinus; } else { swap = ( strInt < op['max-int'] )? strMinus: !strMinus; } } else { swap = ( strInt.length < op['max-int'].length )? strMinus: !strMinus; } } if( swap ) newVal = op['max']; } } // 3桁区切り if( op['locale'] && newVal.length ) { var leftVal = newVal.slice( 0, cursor ); var rightVal = newVal.slice( cursor ); if( !leftVal.match( /\./ ) ) rightVal = this.comma( rightVal, true ); newVal = this.comma( newVal ); cursor = newVal.length - rightVal.length; } } // 適用 if( $(ele).val() != newVal ) { if( backspace && newVal.slice( cursor-1, cursor ) == ',' ) cursor--; $(ele).val( newVal ); if( $(ele).get(0).selectionStart != cursor ) $(ele).get(0).setSelectionRange( cursor, cursor ); } } adjustClass.prototype.comma = function( num, zero ) { var s = String(num).split('.'); if( !zero ) { s[0] = s[0].replace( /^(\-)?0+(\d)/, '$1$2' ); if( s.length > 1 && ( !s[0] || s[0] == '-' ) ) s[0] += '0'; } var ret = String(s[0]).replace( /(\d)(?=(\d\d\d)+(?!\d))/g, '$1,'); if( s.length > 1 ) ret += '.' + s[1]; return ret; } adjustClass.prototype.ruby = function( e, ele ) { var this_ = this; $('.-adjust-ruby-set').each(function(){ var state = $(this).data('-adjust-ruby'); var newVal = $(this).val(); if( state['base'] == newVal ) return true; if( newVal == '' ) { state['kanaEle'].rubyList[state['name']] = ''; $(state['kanaEle']).val( Object.values(state['kanaEle'].rubyList).join(' ').replace( /(^ | $)/, '' ) ); state['base'] = ''; state['pattern'] = {}; $(this).data('-adjust-ruby',state); return true; } for( var namePattern in state['pattern'] ) { var kanaPattern = new RegExp( state['pattern'][namePattern] ); if( !state['kanaEle'].rubyList[state['name']].match( kanaPattern ) ) { delete state['pattern'][namePattern]; } } if( state['pattern'][newVal] != undefined ) { state['kanaEle'].rubyList[state['name']] = state['pattern'][newVal]; $(state['kanaEle']).val( Object.values(state['kanaEle'].rubyList).join(' ').replace( /(^ | $)/, '' ) ); state['base'] = newVal; $(this).data('-adjust-ruby',state); return true; } var addVal = newVal; for( var i = state['base'].length; i >= 0; i-- ) { if( newVal.substr( 0, i ) == state['base'].substr( 0, i ) ) { addVal = newVal.substr( i ); break; } } state['base'] = newVal; var addKana = addVal.replace( /[^  ぁあ-んァー・]/g, '' ); if( addKana != addVal ) { state['pattern'][newVal] = state['kanaEle'].rubyList[state['name']]; $(this).data('-adjust-ruby',state); return true; } if( state['type'] == 'K' ) addKana = this_.hira2kata( addKana ) else if( state['type'] == 'k' ) addKana = this_.zen2han( addKana ); state['kanaEle'].rubyList[state['name']] += addKana; $(state['kanaEle']).val( Object.values(state['kanaEle'].rubyList).join(' ').replace( /(^ | $)/, '' ) ); state['pattern'][newVal] = state['kanaEle'].rubyList[state['name']]; $(this).data('-adjust-ruby',state); return true; }); } adjustClass.prototype.hira2kata = function( src ) { src = this.han2zen( src ); return src.replace(/[\u3041-\u3096]/g, function(match) { var chr = match.charCodeAt(0) + 0x60; return String.fromCharCode(chr); }); } adjustClass.prototype.kata2hira = function( src ) { src = this.han2zen( src ); return src.replace(/[\u30A1-\u30FA]/g, function(match) { var chr = match.charCodeAt(0) - 0x60; return String.fromCharCode(chr); }); } adjustClass.prototype.zen2han = function( src ) { var kanaMap = { "ガ": "ガ", "ギ": "ギ", "グ": "グ", "ゲ": "ゲ", "ゴ": "ゴ", "ザ": "ザ", "ジ": "ジ", "ズ": "ズ", "ゼ": "ゼ", "ゾ": "ゾ", "ダ": "ダ", "ヂ": "ヂ", "ヅ": "ヅ", "デ": "デ", "ド": "ド", "バ": "バ", "ビ": "ビ", "ブ": "ブ", "ベ": "ベ", "ボ": "ボ", "パ": "パ", "ピ": "ピ", "プ": "プ", "ペ": "ペ", "ポ": "ポ", "ヴ": "ヴ", "ヷ": "ヷ", "ヺ": "ヺ", "ア": "ア", "イ": "イ", "ウ": "ウ", "エ": "エ", "オ": "オ", "カ": "カ", "キ": "キ", "ク": "ク", "ケ": "ケ", "コ": "コ", "サ": "サ", "シ": "シ", "ス": "ス", "セ": "セ", "ソ": "ソ", "タ": "タ", "チ": "チ", "ツ": "ツ", "テ": "テ", "ト": "ト", "ナ": "ナ", "ニ": "ニ", "ヌ": "ヌ", "ネ": "ネ", "ノ": "ノ", "ハ": "ハ", "ヒ": "ヒ", "フ": "フ", "ヘ": "ヘ", "ホ": "ホ", "マ": "マ", "ミ": "ミ", "ム": "ム", "メ": "メ", "モ": "モ", "ヤ": "ヤ", "ユ": "ユ", "ヨ": "ヨ", "ラ": "ラ", "リ": "リ", "ル": "ル", "レ": "レ", "ロ": "ロ", "ワ": "ワ", "ヲ": "ヲ", "ン": "ン", "ァ": "ァ", "ィ": "ィ", "ゥ": "ゥ", "ェ": "ェ", "ォ": "ォ", "ッ": "ッ", "ャ": "ャ", "ュ": "ュ", "ョ": "ョ", "。": "。", "、": "、", "ー": "ー", "「": "「", "」": "」", "・": "・" } var reg = new RegExp('(' + Object.keys(kanaMap).join('|') + ')', 'g'); src = this.hira2kata( src ); return src.replace(reg, function (match) { return kanaMap[match]; }).replace(/゛/g, '゙').replace(/゜/g, '゚'); } adjustClass.prototype.han2zen = function( src ) { var kanaMap = { 'ガ': 'ガ', 'ギ': 'ギ', 'グ': 'グ', 'ゲ': 'ゲ', 'ゴ': 'ゴ', 'ザ': 'ザ', 'ジ': 'ジ', 'ズ': 'ズ', 'ゼ': 'ゼ', 'ゾ': 'ゾ', 'ダ': 'ダ', 'ヂ': 'ヂ', 'ヅ': 'ヅ', 'デ': 'デ', 'ド': 'ド', 'バ': 'バ', 'ビ': 'ビ', 'ブ': 'ブ', 'ベ': 'ベ', 'ボ': 'ボ', 'パ': 'パ', 'ピ': 'ピ', 'プ': 'プ', 'ペ': 'ペ', 'ポ': 'ポ', 'ヴ': 'ヴ', 'ヷ': 'ヷ', 'ヺ': 'ヺ', 'ア': 'ア', 'イ': 'イ', 'ウ': 'ウ', 'エ': 'エ', 'オ': 'オ', 'カ': 'カ', 'キ': 'キ', 'ク': 'ク', 'ケ': 'ケ', 'コ': 'コ', 'サ': 'サ', 'シ': 'シ', 'ス': 'ス', 'セ': 'セ', 'ソ': 'ソ', 'タ': 'タ', 'チ': 'チ', 'ツ': 'ツ', 'テ': 'テ', 'ト': 'ト', 'ナ': 'ナ', 'ニ': 'ニ', 'ヌ': 'ヌ', 'ネ': 'ネ', 'ノ': 'ノ', 'ハ': 'ハ', 'ヒ': 'ヒ', 'フ': 'フ', 'ヘ': 'ヘ', 'ホ': 'ホ', 'マ': 'マ', 'ミ': 'ミ', 'ム': 'ム', 'メ': 'メ', 'モ': 'モ', 'ヤ': 'ヤ', 'ユ': 'ユ', 'ヨ': 'ヨ', 'ラ': 'ラ', 'リ': 'リ', 'ル': 'ル', 'レ': 'レ', 'ロ': 'ロ', 'ワ': 'ワ', 'ヲ': 'ヲ', 'ン': 'ン', 'ァ': 'ァ', 'ィ': 'ィ', 'ゥ': 'ゥ', 'ェ': 'ェ', 'ォ': 'ォ', 'ッ': 'ッ', 'ャ': 'ャ', 'ュ': 'ュ', 'ョ': 'ョ', '。': '。', '、': '、', 'ー': 'ー', '「': '「', '」': '」', '・': '・' }; var reg = new RegExp('(' + Object.keys(kanaMap).join('|') + ')', 'g'); return src.replace(reg, function (match) { return kanaMap[match]; }).replace(/゙/g, '゛').replace(/゚/g, '゜'); }