[英]How to validate a user name Using Regular expressions
如果只有,我如何驗證用戶名
有一種情況,我想接受該值僅在未輸入名稱的情況下這意味着用戶想要保留以前的名稱
我使用 REACT.js 這是我的代碼
const alphabet = /^$|^[a-zA-Z ]+$/g;
const numbers = /[0-9]/g;
const isValidNumber = numbers.test(phone)
const isValidName = alphabet.test(name)
if (isValidNumber){
setError(null)
} else{
setError('Please Enter a valid Name');
return
}
if (isValidNumber){
setError(null)
} else{
setError('Please Enter a valid Phone Number');
return
}
我認為下面的代碼會幫助你。
const alphabet = ^[a-zA-Z ]{4,}$;
if (name.text) {
if (!alphabet.test(this.name.text) ) {
setError('Name must be contain more than 3 characters');
return;
} else {
Do something here
}
} else {
setError('Name cannot be empty');
return;
}
我解決了這個問題:
const spaceChar = / {1,}/ ;
const isStartWithSpace = spaceChar.test(name)
if (isValidName){
if (isStartWithSpace){
setError('name Cannot start with a space')
return;
}
setError(null)
} else{
setError('Please Enter a valid Name');
return
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.