'Ternary Operators and Text Input
I am creating an app that is text to sound translator. And I am using input text from react-native-elements, as well as ternary operators. How do I check if the text input box is empty or has any special characters? The app is supposed to detect this and send an alert.
Solution 1:[1]
The following code will show alert when text input is empty and detect special character and show alert, as well as remove the added special character from input
const [inputVal,setInputValue] = React.useState('')
return (
<Input
value={inputVal}
placeholder="Comment"
onChangeText={value => {
if(value==''){
alert('Text Input Empty')
}
let format = /[!"`'#%&,:;<>=@{}~\$\(\)\*\+\/\\\?\[\]\^\|]+/
if(format.test(value)){
alert('Special Character Entered')
let rmvalue = value.slice(0,-1)
setInputValue(rmvalue)
}else setInputValue(value)
}}
/>
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AbdulAzeem |
