我想以这样一种方式来输入文本字段,它会阻止表情符号输入到文本输入字段中。我通过传递对特定路径的控制器进行了验证。这是阻止表情符号的正确方法。
控制器代码。
$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]+"/>
答案 0 :(得分:4)
您可以删除表情符号,然后再使用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>