禁止输入表情符号

 
#import "ViewController.h"
 
@implementation ViewController
{
    IBOutlet UITextView *textinput;
}
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    [textinput setDelegate:self];
}
 
- (void)textViewDidChange:(UITextView *)textView
{
    NSRange textRange = [textView selectedRange];
    [textView setText:[self disable_emoji:[textView text]]];
    [textView setSelectedRange:textRange];
}
 
- (NSString *)disable_emoji:(NSString *)text
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
 
@end
禁止`u-textarea`输入表情符号,可以通过监听输入事件,使用正则表达式过滤掉表情符号。以下是几种不同场景下的实现方法: #### 原生 JavaScript 实现 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <textarea id="u-textarea"></textarea> <script> const textarea = document.getElementById('u-textarea'); textarea.addEventListener('input', function () { this.value = this.value.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, ''); }); </script> </body> </html> ``` 在上述代码中,通过`addEventListener`监听`input`事件,当用户输入内容时,使用正则表达式`/[\uD800-\uDBFF][\uDC00-\uDFFF]/g`匹配表情符号并将其替换为空字符串,从而达到禁止输入表情符号的目的。 #### Vue 项目中实现 ```vue <template> <div> <textarea v-model="inputValue" @input="filterEmoji"></textarea> </div> </template> <script> export default { data() { return { inputValue: '' }; }, methods: { filterEmoji() { this.inputValue = this.inputValue.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, ''); } } }; </script> ``` 在 Vue 项目中,使用`v-model`绑定`textarea`的值,通过`@input`监听输入事件,在`filterEmoji`方法中使用相同的正则表达式过滤表情符号。 #### React 项目中实现 ```jsx import React, { useState } from 'react'; const UTextarea = () => { const [inputValue, setInputValue] = useState(''); const handleInput = (e) => { const value = e.target.value.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, ''); setInputValue(value); }; return ( <textarea value={inputValue} onChange={handleInput} /> ); }; export default UTextarea; ``` 在 React 项目中,使用`useState`来管理`textarea`的值,通过`onChange`事件监听输入,在`handleInput`方法中过滤表情符号并更新状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值