Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  182] [ 6]  / answers: 1 / hits: 21552  / 6 Years ago, wed, february 14, 2018, 12:00:00

How to stop entering whitespace in password input field in React Native?



password might be any characters including space.



I tried this: validator.js



const extract = (str, pattern) => (str.match(pattern) || []).pop() || '';

export function removeWhiteSpace(str) {
return extract(str, '/^S*$/;');
}


login.js



passwordHandle(value){
this.setState({
password:removeWhiteSpace(value)
})
console.log(removeWhiteSpace(value))
}


render()



<View style={{paddingBottom:25}}>
<TextField
label='Password'
type='password'
value={password}
error={errors.password}
icon
onChange={this.passwordHandle}/>
<Image
source={require('../../../assets/img/lock.png')}
style={styles.icon} />
</View>


But it doesn't work.
It only executes the '/^S*$/;' from removeWhiteSpace.


More From » reactjs

 Answers
9

This simple regex should work using .replace function :



passwordHandle(value){
this.setState({
password: value.replace(/s/g, '')
})
}

[#55155] Monday, February 12, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
masonm

Total Points: 167
Total Questions: 87
Total Answers: 103

Location: Rwanda
Member since Wed, Jun 8, 2022
2 Years ago
masonm questions
;