当前位置:网站首页>JS text box loses focus to modify width text and symbols
JS text box loses focus to modify width text and symbols
2022-06-28 05:01:00 【fengyehongWorld】
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<hr>
Half angle digit <input type="text" data-validate="number">
<hr>
Half width mailbox <input type="text" data-validate="email">
<hr>
Time with semicolon <input type="text" data-validate="time">
</body>
<script src="../jquery.min.js"></script>
<script type="module" src="./js/17-main.js"></script>
</html>
Main method
import typeHandleMap from './17-utils.js'
$(function() {
// Give all that contain data-validate The text input box is bound to the focus loss event
for(const element of Array.from($('input:text')).values()) {
// When the text input box does not have data-validate When attributes
const validatetype = element.dataset?.validate;
if(!validatetype) {
return;
}
$(element).on('blur', ({
target}) => {
// Get the type of validation conversion
const validatetypeList = target.dataset?.validate.split(',');
for(const validatetype of validatetypeList.values()) {
// Execute the corresponding method according to the type to be verified , Put the converted text back into the text box
const invokeMethod = typeHandleMap.get(validatetype);
target.value = invokeMethod.call(this, target.value);
}
});
}
});
Encapsulated tool class
class textHandleUtils {
// Correspondence between full width and half width Kanas
zen2HanKanaMap = {
'ガ': 'ガ', 'ギ': 'ギ', 'グ': 'グ', 'ゲ': 'ゲ', 'ゴ': 'ゴ',
'ザ': 'ザ', 'ジ': 'ジ', 'ズ': 'ズ', 'ゼ': 'ゼ', 'ゾ': 'ゾ',
'ダ': 'ダ', 'ヂ': 'ヂ', 'ヅ': 'ヅ', 'デ': 'デ', 'ド': 'ド',
'バ': 'バ', 'ビ': 'ビ', 'ブ': 'ブ', 'ベ': 'ベ', 'ボ': 'ボ',
'パ': 'パ', 'ピ': 'ピ', 'プ': 'プ', 'ペ': 'ペ', 'ポ': 'ポ',
'ヴ': 'ヴ', 'ヷ': 'ヷ', 'ヺ': 'ヺ',
'ア': 'ア', 'イ': 'イ', 'ウ': 'ウ', 'エ': 'エ', 'オ': 'オ',
'カ': 'カ', 'キ': 'キ', 'ク': 'ク', 'ケ': 'ケ', 'コ': 'コ',
'サ': 'サ', 'シ': 'シ', 'ス': 'ス', 'セ': 'セ', 'ソ': 'ソ',
'タ': 'タ', 'チ': 'チ', 'ツ': 'ツ', 'テ': 'テ', 'ト': 'ト',
'ナ': 'ナ', 'ニ': 'ニ', 'ヌ': 'ヌ', 'ネ': 'ネ', 'ノ': 'ノ',
'ハ': 'ハ', 'ヒ': 'ヒ', 'フ': 'フ', 'ヘ': 'ヘ', 'ホ': 'ホ',
'マ': 'マ', 'ミ': 'ミ', 'ム': 'ム', 'メ': 'メ', 'モ': 'モ',
'ヤ': 'ヤ', 'ユ': 'ユ', 'ヨ': 'ヨ',
'ラ': 'ラ', 'リ': 'リ', 'ル': 'ル', 'レ': 'レ', 'ロ': 'ロ',
'ワ': 'ワ', 'ヲ': 'ヲ', 'ン': 'ン',
'ァ': 'ァ', 'ィ': 'ィ', 'ゥ': 'ゥ', 'ェ': 'ェ', 'ォ': 'ォ',
'ッ': 'ッ', 'ャ': 'ャ', 'ュ': 'ュ', 'ョ': 'ョ',
'.': '。', '、': '、', 'ー': 'ー', '「': '「', '」': '」', '・': '・'
};
// Correspondence between half width and full width Kanas
han2ZenKanaMap = {
'ガ': 'ガ', 'ギ': 'ギ', 'グ': 'グ', 'ゲ': 'ゲ', 'ゴ': 'ゴ',
'ザ': 'ザ', 'ジ': 'ジ', 'ズ': 'ズ', 'ゼ': 'ゼ', 'ゾ': 'ゾ',
'ダ': 'ダ', 'ヂ': 'ヂ', 'ヅ': 'ヅ', 'デ': 'デ', 'ド': 'ド',
'バ': 'バ', 'ビ': 'ビ', 'ブ': 'ブ', 'ベ': 'ベ', 'ボ': 'ボ',
'パ': 'パ', 'ピ': 'ピ', 'プ': 'プ', 'ペ': 'ペ', 'ポ': 'ポ',
'ヴ': 'ヴ', 'ヷ': 'ヷ', 'ヺ': 'ヺ',
'ア': 'ア', 'イ': 'イ', 'ウ': 'ウ', 'エ': 'エ', 'オ': 'オ',
'カ': 'カ', 'キ': 'キ', 'ク': 'ク', 'ケ': 'ケ', 'コ': 'コ',
'サ': 'サ', 'シ': 'シ', 'ス': 'ス', 'セ': 'セ', 'ソ': 'ソ',
'タ': 'タ', 'チ': 'チ', 'ツ': 'ツ', 'テ': 'テ', 'ト': 'ト',
'ナ': 'ナ', 'ニ': 'ニ', 'ヌ': 'ヌ', 'ネ': 'ネ', 'ノ': 'ノ',
'ハ': 'ハ', 'ヒ': 'ヒ', 'フ': 'フ', 'ヘ': 'ヘ', 'ホ': 'ホ',
'マ': 'マ', 'ミ': 'ミ', 'ム': 'ム', 'メ': 'メ', 'モ': 'モ',
'ヤ': 'ヤ', 'ユ': 'ユ', 'ヨ': 'ヨ',
'ラ': 'ラ', 'リ': 'リ', 'ル': 'ル', 'レ': 'レ', 'ロ': 'ロ',
'ワ': 'ワ', 'ヲ': 'ヲ', 'ン': 'ン',
'ァ': 'ァ', 'ィ': 'ィ', 'ゥ': 'ゥ', 'ェ': 'ェ', 'ォ': 'ォ',
'ッ': 'ッ', 'ャ': 'ャ', 'ュ': 'ュ', 'ョ': 'ョ',
'。': '.', '、': '、', 'ー': 'ー', '「': '「', '」': '」', '・': '・', '-': 'ー',
};
constructor() {
}
// Half width to full width ( English sign )
han2ZenAlphaNumberSymbol(value) {
return value.replace(/[!-~]/g, function (s) {
return String.fromCharCode(s.charCodeAt(0) + 0xFEE0);
});
}
// Full width to half width ( English sign )
zen2HanAlphaNumberSymbol(value) {
let s = value.replace(/[!-~]/g, function (s) {
return String.fromCharCode(s.charCodeAt(0) - 0xFEE0);
});
return s.replace(/[‐-―ー]/g, '-');
}
// Full width kana to half width kana
zen2HanKana(value) {
const reg = new RegExp('(' + Object.keys(this.zen2HanKanaMap).join('|') + ')', 'g');
return value.replace(reg, function (match) {
return kanaMap[match]; }).replace(/゛/g, '゙').replace(/゜/g, '゚');
}
// Half width kana to full width kana
han2ZenKana(value) {
const reg = new RegExp('(' + Object.keys(this.han2ZenKanaMap).join('|') + ')', 'g');
return value.replace(reg, function (match) {
return kanaMap[match]; }).replace(/゙/g, '゛').replace(/゚/g, '゜');
}
// Delete numbers and - outside
removeNonHyphenNumber(value) {
return value.replace(/[^\d-]/g, '');
}
// Delete numbers and : outside
removeNonColonNumber(value) {
return value.replace(/[^\d:]/g, '');
}
// Delete other than numbers
removeNonNumber(value) {
return value.replace(/[^\d]/g, '');
}
}
const textHandleClass = new textHandleUtils();
// The processing method corresponding to the type
const typeHandleMap = new Map([
// Full angle
['zenkaku', (value) => {
const value1 = textHandleClass.han2ZenKana(value);
return textHandleClass.han2ZenAlphaNumberSymbol(value1);
}],
// Half angle
['hankaku', (value) => {
const value1 = textHandleClass.zen2HanKana(value);
return textHandleClass.zen2HanAlphaNumberSymbol(value1);
}],
// mailbox
['email', (value) => {
return textHandleClass.zen2HanAlphaNumberSymbol(value);
}],
// Telephone
['tel', (value) => {
const value1 = textHandleClass.zen2HanAlphaNumberSymbol(value);
return textHandleClass.removeNonHyphenNumber(value1);
}],
// Zip code
['postCode', (value) => {
const value1 = textHandleClass.zen2HanAlphaNumberSymbol(value);
return textHandleClass.removeNonHyphenNumber(value1);
}],
// user name
['userName', (value) => {
const value1 = textHandleClass.zen2HanAlphaNumberSymbol(value);
return textHandleClass.han2ZenKana(value1);
}],
// Time
['time', (value) => {
const value1 = textHandleClass.zen2HanAlphaNumberSymbol(value);
return textHandleClass.removeNonColonNumber(value1);
}],
// Numbers
['number', (value) => {
const value1 = textHandleClass.zen2HanAlphaNumberSymbol(value);
return textHandleClass.removeNonNumber(value1);
}]
]);
export default typeHandleMap;
effect
Before losing focus 
After losing focus 
边栏推荐
- Severe tire damage: the first rock band in the world to broadcast live on the Internet
- [CSP-J2020] 优秀的拆分
- [early knowledge of activities] list of recent activities of livevideostack
- Learning Tai Chi Maker - mqtt Chapter 2 (V) heartbeat mechanism
- Generate QR code in wechat applet
- Necessary skills for test and development: actual combat of security test vulnerability shooting range
- Mask's miserable and inspirational childhood, who is introverted by campus violence
- Sword finger offer 49 Ugly number (three finger needling technique)
- Congratulations to myself, official account has more than ten thousand fans
- 羧酸研究:Lumiprobe 磺基花青7二羧酸
猜你喜欢

论文详读:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
![[noip2002 popularization group] cross the river pawn](/img/6c/31fa210e08c7fd07691a1c5320154e.png)
[noip2002 popularization group] cross the river pawn

Severe tire damage: the first rock band in the world to broadcast live on the Internet

2022 low voltage electrician examination questions and answers

What to do when MySQL changes the password and reports an error

Generate QR code in wechat applet

Project practice! Teach you JMeter performance test hand in hand

TACo:一种关于文字识别的数据增强技术

Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
![[NOIP2002 普及组] 过河卒](/img/6c/31fa210e08c7fd07691a1c5320154e.png)
[NOIP2002 普及组] 过河卒
随机推荐
CUPTI error: CUPTI could not be loaded or symbol could not be found.
信息学奥赛一本通 1360:奇怪的电梯(lift)
Sword finger offer 53 - I. find the number I in the sorted array (improved bisection)
代码理解:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
Assembly common instructions
电源插座是如何传输电的?困扰小伙伴这么多年的简单问题
灵活的IP网络测试工具——— X-Launch
native关键字的作用
IP数据报的发送和转发过程
2022年低压电工考题及答案
BioVendor sRAGE Elisa试剂盒化学性质和技术研究
改性三磷酸盐研究:Lumiprobe氨基-11-ddUTP
Distributed transaction - Final consistency scheme based on message compensation (local message table, message queue)
Unity delegate
Unity delegate
Interview: what are the similarities and differences between abstract classes and interfaces?
?位置怎么写才能输出true
程序员-放羊娃
Learning Tai Chi Maker - mqtt Chapter II (VI) mqtt wills
TACo:一种关于文字识别的数据增强技术