'Regex with all special symbols
I need regex that will allow only Latin characters, digits and all other symbols(but not whitespace)
thanks!
UPDATE:
private boolean loginPassHasCorrectSymbols(String input){
if (input.matches("[A-Za-z0-9\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\>\=\?\@\[\]\{\}\\\^\_\`\~]+$")){
return true;
}
return false;
}
Solution 1:[1]
How about everything not a whitespace?
"^\S+$"
Solution 2:[2]
I did this and it works for me .
Either you can block whitespace by mentioning it on Edittext, or you can block on editetext.addtextChangeListner too by pragmatically .
1>
android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm,_,-"
2>
etNewPassword.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (etNewPassword.getText().toString().contains(" ")) {
etNewPassword.setText(etNewPassword.getText().toString().replace(" ", ""));
int iLength = etNewPassword.getText().toString().length();
etNewPassword.setSelection(iLength);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Let me know if any concern.
Solution 3:[3]
For find any symbol except whitespace, you can use this code. I hope you find it useful
public static boolean hasAnySymbolExceptWhitespace(String string){
return Pattern.matches("(?=.*[^a-zA-Z0-9] ).*", string);
}
Solution 4:[4]
Smooth Kotlin solution which allows English letters with some default symbols. Cyrillic or any other language symbols won't be allowed.
//Allows english with usual symbols
private fun hasNonAllowedSymbols(input: String) : Boolean {
val regex = "[a-zA-Z0-9\\s-#,/~`'!@$%^&*()_+={}|;<>.?:\"\\[\\]\\\\]*"
val pattern = Pattern.compile(regex)
return !pattern.matcher(input).matches()
}
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 | Shawn Mehan |
| Solution 2 | Tarit Ray |
| Solution 3 | fvaldivia |
| Solution 4 | Andrey Kijonok |
