[英]restrict special characters in reactNative TextInput
我試圖阻止我的 TextInput 獲取$,%,^,&,(,) etc
等值。 基本上我的 TextInput 應該只允許字母。 我的方法如下。 但我仍然可以輸入這些其他字符。 如何防止來自 TextInput 的特殊字符
restrict(event) {
const regex = new RegExp("^[a-zA-Z]+$");
const key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
}
<TextInput
underlineColorAndroid='transparent'
allowFontScaling={false}
style={styles.questionText}
onKeyPress={e => this.restrict(e)}
value={firstNameState}
/>
我試圖阻止我的TextInput獲得$,%,^,&,(,) etc
等值。 基本上我的TextInput應該只允許字母。 我的方法如下。 但是我仍然可以輸入其他字符。 我如何防止TextInput中的特殊字符
restrict(event) {
const regex = new RegExp("^[a-zA-Z]+$");
const key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
}
<TextInput
underlineColorAndroid='transparent'
allowFontScaling={false}
style={styles.questionText}
onKeyPress={e => this.restrict(e)}
value={firstNameState}
/>
我試圖阻止我的TextInput獲得$,%,^,&,(,) etc
等值。 基本上我的TextInput應該只允許字母。 我的方法如下。 但是我仍然可以輸入其他字符。 我如何防止TextInput中的特殊字符
restrict(event) {
const regex = new RegExp("^[a-zA-Z]+$");
const key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
}
<TextInput
underlineColorAndroid='transparent'
allowFontScaling={false}
style={styles.questionText}
onKeyPress={e => this.restrict(e)}
value={firstNameState}
/>
我必須通過這行代碼來阻止特殊字符。
var format = /[!@#$%^&*()_+-=[]{};':"\\|,.<>/?]+/;
如果(格式。測試(字符串)){}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.