【问题标题】:How to block emoticons from input text field in codeignitier如何在codeigniter中阻止输入文本字段中的表情符号
【发布时间】:2018-12-10 06:25:19
【问题描述】:

我想以这样一种方式制作输入文本字段,它会阻止在文本输入字段中输入表情符号。我已经通过传递对特定路线的控制器进行了验证。这是阻止表情符号的正确方法吗?

控制器代码。

$this->form_validation->set_rules('registration', 'Registration', 'required|alpha_numeric');
           $this->form_validation->set_rules('odometer', 'Odometer', 'required|alpha_numeric');

查看代码:

<input type="text" name="odometer" placeholder="Odometer" class="form-control profileControl  odometer required number" id="odometer-<?php echo $vehicle_count; ?>" value="<?php echo $vehicle->intCarOdometer ?>"/>

<input type="text" placeholder="Registration" name="registration" class="form-control profileControl  registration required" id="registration-<?php echo $vehicle_count; ?>" value="<?php echo $vehicle->vchCarRegistration ?>" pattern="[a-zA-Z0-9]+"/>

【问题讨论】:

  • 是的,它可以工作,但您也可以在使用 jquery 点击发送到服务器之前将其删除
  • 文本输入的字母数字验证是​​阻止表情符号的正确方法?
  • 不,它可能存在一些需要的字符。
  • 我可以在视图中添加这个脚本吗?
  • 是的,现在是onclick,您可以将其更改为onkeyup input

标签: codeigniter model-view-controller codeigniter-3


【解决方案1】:

您可以在使用 jquery 发送到服务器之前删除表情符号。

测试此代码。

var ranges = [
  '\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
  '\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
  '\ud83d[\ude80-\udeff]'  // U+1F680 to U+1F6FF
];

setTimeout(removeInvalidChars, 100);
function removeInvalidChars() {
  var str = $('.valid-text').val();
 
  str = str.replace(new RegExp(ranges.join('|'), 'g'), '');
  $(".valid-text").val(str);
 setTimeout(removeInvalidChars, 100);
}
<script type="text/javascript"  src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<input id="myinput" class="valid-text" type="email" name="myinput" value=" ? value 1 2 3 ? / "></input>

【讨论】:

  • 10 毫秒的超时时间太快了(性能问题)。 100 可能是更好的价值
  • 100 很长,可能在这 100 毫秒之间用户保存数据并将其发送到服务器。
  • 我认为无法在输入中键入一些文本,然后在 100 毫秒内通过键盘和鼠标发送表单。更不用说在提交表单时可以删除表情符号。
  • 现在是 100ms。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多