有一个好的方法来禁用“智能标点符号”的iOS 11苹果键盘生成-在登录表单-用户名字段,特别是?
问题是,我们的用户名中有带有撇号的用户。在iOS 11上键入他们的用户名,不知道他们无法登录的unicode的微妙之处。
理想情况下,我们可以指示这样的用户禁用智能引号或按下撇号键键入正确的字符--但我正在为幼儿开发教育软件,这是不可能的。
更复杂的是,还有一小部分用户的用户名中有实际的卷曲单引号,所以简单的替换图是行不通的--而且我们无法规范用户名,因为它们来自一些我们无法控制的外部系统/没有太多发言权。
发布于 2018-03-06 21:06:07
不幸的是,根据此MDN页面,没有已知的Webkit <input>或<textarea>属性来禁用智能引号,但是您可以用我从QA:如何禁用浏览器中textarea字段的智能引号?中派生的脚本对其进行修补。
window.addEventListener('DOMContentLoaded', attachFilterToInputs );
function attachFilterToInputs() {
var textInputs = document.querySelectorAll( 'input[type=text], input[type=password], input[type=email], input[type=search], input[type=url], textarea, *[contenteditable=true]' );
for( var i = 0; i < textInputs.length; i++ ) {
textInputs[i].addEventListener( 'keypress', preventPretentiousPunctuation );
}
}
var conversionMap = createConversionMap();
function createConversionMap() {
var map = {};
// Open-quotes: http://www.fileformat.info/info/unicode/category/Pi/list.htm
map[ 0x2018 ] = '\'';
map[ 0x201B ] = '\'';
map[ 0x201C ] = '"';
map[ 0x201F ] = '"';
// Close-quotes: http://www.fileformat.info/info/unicode/category/Pf/list.htm
map[ 0x2019 ] = '\'';
map[ 0x201D ] = '\"';
// Primes: http://www.fileformat.info/info/unicode/category/Po/list.htm
map[ 0x2032 ] = '\'';
map[ 0x2033 ] = '"';
map[ 0x2035 ] = '\'';
map[ 0x2036 ] = '"';
map[ 0x2014 ] = '-'; // iOS 11 also replaces dashes with em-dash
map[ 0x2013 ] = '-'; // and "--" with en-dash
return map;
}
function preventPretentiousPunctuation( event ) {
if( event.key.length != 1 ) return;
var code = event.key.codePointAt(0);
var replacement = conversionMap[ code ];
if( replacement ) {
event.preventDefault();
document.execCommand( 'insertText', 0, replacement );
}
} 发布于 2018-04-18 03:33:20
感谢Dai给出了答案的大纲。不幸的是,当我们实现他们的解决方案并在iOS 11设备上进行测试时,我们发现keypress事件侦听器根本没有触发。我们使用input事件和regex字符串替换重新工作了他们的解决方案,发现这对我们确实有效。
这是我们的变体,使用jQuery作为选择器和一个较短的替换列表。
<script type="text/javascript">
// Here are the reference to Unicode Characters in the Punctuation
// Open-quotes: http://www.fileformat.info/info/unicode/category/Pi/list.htm
// Close-quotes: http://www.fileformat.info/info/unicode/category/Pf/list.htm
window.addEventListener('DOMContentLoaded', attachFilterToInputs);
// Patch the iOS Smart Punctuation functionality on restricted field(s),
// so that the user types acceptable characters.
function attachFilterToInputs() {
try {
$("[name$='MyProtectedFieldName']").on('input', preventPretentiousPunctuation);
}
catch (err) {
// do nothing
}
}
function preventPretentiousPunctuation(event) {
try {
var str = $(this).val();
var replacedStr = "";
replacedStr = str.replace(/[\u2018\u2019\u201C\u201D]/g,
(c) => '\'\'""'.substr('\u2018\u2019\u201C\u201D'.indexOf(c), 1));
if (str !== replacedStr) {
$(this).val(replacedStr);
}
}
catch (err) {
// do nothing
}
}
</script>添加作为一个回答,因为我没有代表可以评论。
https://stackoverflow.com/questions/48678359
复制相似问题