/*
* 入力文字列を自然数かを判定する
* @param numString 判定文字列
*/
function castNaturalNumber(numString) {
let numberPattern = new RegExp(/^[0-9]+$/);
if(numberPattern.test(numString)) {
let num = parseInt(numString, 10);
if (Number.isNaN(num) || num <= 0) {
return Number.NaN;
} else {
return num;
}
} else {
return Number.NaN;
}
}
/*
* 入力文字列を整数かを判定する
* @param numString 判定文字列
*/
function castNumber(numString) {
let numberPattern = new RegExp(/^[0-9]+$/);
if(numberPattern.test(numString)) {
let num = parseInt(numString, 10);
if (Number.isNaN(num)) {
return Number.NaN;
} else {
return num;
}
} else {
return Number.NaN;
}
}
/*
* 指定されたIDにメッセージを出力する
* @param id 出力するオブジェクトID
* @param message 出力メッセージ
*/
function setMessage(id, message) {
let messageSpace = document.getElementById(id);
if (messageSpace === null) {
alert(id + 'が見つかりません');
} else {
messageSpace.innerHTML = message;
}
}
/*
* jquery dialogを表示する
* @param id 出力するオブジェクトID
* @param title ダイアログのタイトル
* @param message 出力メッセージ
*
*/
function showDialog(id, title, message) {
let messageDialog = document.getElementById(id);
if (messageDialog === null) {
alert(message);
} else {
messageDialog.innerHTML = message;
$('#'+id).dialog({
modal:true,
postion:{
my: "center top",
at: "center top",
of: '#'+id
},
title: title,
buttons: {
"OK": function() {
messageDialog.innerHTML = '';
$(this).dialog("close");
}
},
});
return false;
}
}
/*
* 全角半角混在文字列のバイト数をチェックする
* @testee チェック対象の文字列
* @maxbyte 最大バイト数
*/
function checkItemLength(testee, maxBytes) {
if (isEmpty(testee)) {
return false;
} else if (testee.length == 0) {
return true;
} else {
let strBytes = getSjisBytes(testee);
if (strBytes > maxBytes) {
return false;
} else {
return true;
}
}
}
/*
* 文字列の文字コードがSJISかを調べる
* encode.jpを利用 https://github.com/polygonplanet/encoding.js
* @param チェック対象の文字列
*/
function isSjis(testee) {
if (isEmpty(testee)) {
return false;
} else if (testee.length == 0) {
return true;
} else {
let unicodeChars = testee.split('').map((v) => v.charCodeAt());
let sjisChars = Encoding.convert(unicodeChars, 'SJIS', 'UNICODE');
if (Encoding.detect(sjisChars) == 'SJIS') {
return true;
} else {
return false;
}
}
}
/*
* 文字列のJISX0208チェックとSHIFT_JIS変換後のバイト数を計算
* @str 計算対象文字列
* @return バイト数
* -1:SJIS以外
* -2:CP932文字
*/
function getSjisBytes(testee) {
if (isEmpty(testee)) {
return 0;
} else if (testee.length == 0) {
return 0;
} else {
/*
* encoding.jsで変換すると非マッピング文字は?(u003f)に変換される
* 変換前後の?の数を比較し、不一致をSJIS以外としてエラーとする
*/
let pre003f = (testee.match(/\u003f/g) || []).length; // 基本的に禁止文字チェック後なので0
let unicodeChars = testee.split('').map((v) => v.charCodeAt());
let sjisChars = Encoding.convert(unicodeChars, 'SJIS', 'UNICODE');
let after003f = 0;
for (let i = 0; i < sjisChars.length; i++) {
if (sjisChars[i] == 0x3F) after003f++;
/*
* encoding.jsがCP932を利用しているみたいなので、
* NEC・IBM拡張、NEC選定IBM拡張をNGとする
* EDIで許可されているのはJISX0208のみ
*/
if (i + 1 < sjisChars.length) {
if ((sjisChars[i] >= 0x81 && sjisChars[i] <= 0x9f) || (sjisChars[i] >= 0xe0 && sjisChars[i] <= 0xfc)) {
if (sjisChars[i] == 0x87) { // NEC拡張文字
if (0x40 <= sjisChars[i+1] && sjisChars[i+1] <= 0x9f) {
return -1;
}
} else if (sjisChars[i] == 0xFA) { // IBM拡張文字
if (0x40 <= sjisChars[i+1] && sjisChars[i+1] <= 0xFF) {
return -1;
}
} else if (sjisChars[i] == 0xFB) {
if (0x40 <= sjisChars[i+1] && sjisChars[i+1] <= 0xFF) {
return -1;
}
} else if (sjisChars[i] == 0xFC) {
if (0x40 <= sjisChars[i+1] && sjisChars[i+1] <= 0x4F) {
return -1
}
} else if (sjisChars[i] == 0xED) { // NEC選定IBM拡張文字
if (0x40 <= sjisChars[i+1] && sjisChars[i+1] <= 0xFF) {
return -1;
}
} else if (sjisChars[i] == 0xEE) {
if(0x40 <= sjisChars[i+1] && sjisChars[i+1] <= 0xFF) {
return -1;
}
}
i = i + 1;
}
}
}
if (pre003f != after003f) {
return -1;
} else {
return sjisChars.length;
}
}
}
/*
* 入力制限文字チェック
*/
function limitCheck(strobj) {
//入力制限文字コードの配列
const climit = [0x22, 0x8167, 0x8168, 0x7C, 0x2C, 0x8141, 0x8143, 0x3F, 0x2A, 0x23, 0x5C, 0x5F, 0x27, 0x25,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x1E, 0x1F];
let checkFlag = true;
let str = strobj.value;
for (var i = 0; i < str.length; i++) {
let c = str.charCodeAt(i);
let index = climit.indexOf(c);
if (index != -1) {
checkFlag = false;
break;
}
}
return checkFlag;
}
/*
* 空判定
* @testee 検査対象文字列
*/
function isEmpty(testee) {
return (typeof testee === 'undefined' || testee === null) ? true : false;
}
/*
* 禁止文字を含んでいるか
* @testee 検査対象文字列
* ”:u201D “:u201C 、:u3001 ,:uFF0C
*/
function containNgCharacter(testee) {
const NgCharacterPtn = new RegExp('[\u201d\u201c\u3001\uff0c\x22\x7c\x2c\x3f\x2a\x23\x5c\x5f\x27\x25\x00-\x1F]+');
return NgCharacterPtn.test(testee);
}
/*
* 波ダッシュ対応
* @testee 検査対象文字列
*/
function containWebdashCharacter(testee) {
const NgCharacterPtn = new RegExp('[\u301c\u2016\u2212\u00a2\u00a3\u00ac\u2014]+');
return NgCharacterPtn.test(testee);
}
/*
* 全角・半角混合項目入力(禁止文字・SJIS・バイト数)チェック
* @param formObj フォームオブジェクト
* @param itemName 項目名
* @param itemMaxSize 最大バイト数
* @return
*/
function checkJapaneseItem(formObj, itemName, itemMaxSize) {
let errorMessage = '';
if (isEmpty(formObj)) {
alert('フォームオブジェクトがありません');
return false;
} else {
let errorId = formObj.id + 'Error';
setMessage(errorId, '');
formObj.style.backgroundColor = 'transparent';
let testee = formObj.value;
if (testee.length == 0) {
return true;
} else {
if (containNgCharacter(testee)) {
errorMessage = '\u26a0\uFE0F' + ' ' + itemName + 'に利用できない文字(?*%#,|\\_\'\"、,”“)が含まれています
';
}
let num = getSjisBytes(testee);
if (containWebdashCharacter(testee) || num < 0) {
errorMessage += itemName + 'に利用できない全角文字が含まれています
';
} else if (num > itemMaxSize) {
errorMessage += itemName + 'は全角' + (itemMaxSize / 2) + '文字、半角' + itemMaxSize + '文字以内でご入力ください';
} else {}
if (errorMessage.length == 0) {
return true;
} else {
if (errorMessage.match(/.*
$/) != null) {
errorMessage = errorMessage.substring(0, errorMessage.length - 4);
}
}
setMessage(errorId, errorMessage);
formObj.focus();
return false;
}
}
}
/*
* 全角・半角混合項目入力(禁止文字・SJIS・バイト数)チェック
* @param formObj フォームオブジェクト
* @param itemName 項目名
* @param itemMaxSize 最大バイト数
* @param focusId チェックOK後の遷移先ID
* @return
*/
function checkJapaneseItemAfterMoveFocus(formObj, itemName, itemMaxSize, focusId) {
let obj = document.getElementById(focusId);
if (isEmpty(obj)) {
setMessage(id, '遷移先のオブジェクトが見つかりません');
return false;
}
let errorMessage = '';
if (isEmpty(formObj)) {
setMessage(id, 'フォームオブジェクトが見つかりません');
return false;
} else {
let errorId = formObj.id + 'Error';
setMessage(errorId, '');
formObj.style.backgroundColor = 'transparent';
let testee = formObj.value;
if (testee.length == 0) {
obj.focus();
return true;
} else {
if (containNgCharacter(testee)) {
errorMessage = '\u26a0\uFE0F' + ' ' + itemName + 'に利用できない文字(?*%#,|\\_\'\"、,”“)が含まれています
';
}
let num = getSjisBytes(testee);
if (containWebdashCharacter(testee) || num < 0) {
errorMessage += itemName + 'に利用できない全角文字が含まれています
';
} else if (num > itemMaxSize) {
errorMessage += itemName + 'は全角' + (itemMaxSize / 2) + '文字、半角' + itemMaxSize + '文字以内でご入力ください';
} else {}
if (errorMessage.length == 0) {
obj.focus();
return true;
} else {
if (errorMessage.match(/.*
$/) != null) {
errorMessage = errorMessage.substring(0, errorMessage.length - 4);
}
}
setMessage(errorId, errorMessage);
formObj.focus();
return false;
}
}
}
/*
* メール形式チェック(クロネコWebコレクト形式)
* @param formObj フォームオブジェクト
* @param itemName 項目名
* 利用可能な記号はRFCから引用
*/
function checkMailAddress(formObj, itemName) {
let errorMessage = '';
if (isEmpty(formObj)) {
alert('フォームオブジェクトがありません');
} else {
let errorId = formObj.id + 'Error';
setMessage(errorId, '');
formObj.style.backgroundColor = 'transparent';
let testee = formObj.value;
if (testee.length == 0) {
return;
} else {
let atmarkNum = (testee.match(/@/g) || []).length;
if (atmarkNum > 1) {
errorMessage = 'メールアドレスに「@」は1つしか利用できません';
} else if (atmarkNum == 0) {
errorMessage = 'メールアドレスには「@」が1つ必要です';
} else {
let mailStrings = testee.split('@');
let account = mailStrings[0];
let accountPattern = new RegExp(/^[a-zA-z0-9!#%&'=_`~\/\-\$\*\+\^\?\{\}\|]+(\.[a-zA-z0-9!#%&'=_`~\/\-\$\*\+\^\?\{\}\|]+)*$/);
if (! accountPattern.test(account)) {
errorMessage = '「@」前の文字に利用できない文字が含まれています';
}
let domain = mailStrings[1];
let domainPattern = new RegExp(/^[a-zA-z\.]+$/);
if (! domainPattern.test(domain)) {
if (errorMessage.length != 0) {
errorMessage += '
「@」後の文字は、英字と.のみ利用可能です';
} else {
errorMessage = '「@」後の文字は、英字と.のみ利用可能です';
}
}
}
if (errorMessage.length != 0) {
setMessage(errorId, errorMessage);
formObj.focus();
}
}
}
}
/*
* 電話番号チェック
* @param formObj フォームオブジェクト
* @param id エラー出力先id
*/
function checkPhoneNumber(formObj, id) {
if (formObj.value.length == 0) {
return;
}
if (formObj.value.match(/^[0-9]+$/) === null) {
setMessage(id, '電話番号は半角数値でご入力ください');
formObj.focus();
} else {
setMessage(id, '');
formObj.style.backgroundColor = 'transparent';
}
}
/*
* 電話番号チェック
* @param formObj フォームオブジェクト
* @param id エラー出力先id
* @param focusId チェックOK後の遷移先ID
*/
function checkPhoneNumberAfterMoveFocus(formObj, id, focusId) {
let obj = document.getElementById(focusId);
if (isEmpty(obj)) {
setMessage(id, '遷移先のオブジェクトが見つかりません');
return;
}
if (formObj.value.length == 0) {
obj.focus();
return;
}
if (formObj.value.match(/^[0-9]+$/) === null) {
setMessage(id, '電話番号は半角数値でご入力ください');
formObj.focus();
} else {
setMessage(id, '');
formObj.style.backgroundColor = 'transparent';
obj.focus();
}
}
/*
* URLからパラメータを取得する
* URLオブジェクトは未対応ブラウザの懸念があるので未使用
* @param loc locationオブジェクト
*/
function getUrlParam(loc) {
let line = loc.attr('search'); // ?key=value...
var results = new Array();
if (! isEmpty(line) && line.length != 0) {
line = line.substring(1);
let params = line.split('&');
for (let i = 0; i < params.lnegth; i++) {
let keyValue = params[i].split('=');
if (keyValue.length == 2) {
results[keyValue[0]] = keyValue[1];
}
}
}
return results;
}
/*
* フォームオブジェクトにhiddenを追加
* @param formObj フォームオブジェクト
* @param key キー名
* @param value 値
*/
function addHidden(formObj, key, value) {
if (! isEmpty(key)) {
if (isEmpty(value)) value = '';
$('').attr({
'type': 'hidden',
'name': key,
'value': value
}).appendTo(formObj);
}
}
/*
* ページ内のhiddenをkey=valueに変換
*/
function getHiddens() {
var results = {};
var hdns = $('input:hidden');
for (key in hdns) {
results[hdns[key].name] = hdns[key].value;
}
return results;
}