i'm making a program and theres some sql going on so i decided its best to limit the textfields that are potentially dangerous to a-z, A-Z, 0-9 and thats it
the only thing i could find was maskformatting but that is limiting the number of characters i can enter
like when i
x = new MaskFormatter("*****");
x.setValidCharacters("a, b, ...")
i am forced to make those fields exactly 5 characters long but at least it limits the characters i can enter
does anyone have a solution?
4 replies to this topic
#1
Posted 06 January 2011 - 08:59 AM
|
|
|
#2
Posted 06 January 2011 - 09:28 AM
First of all, I assume you want this to prevent SQL injection. By using prepared statements only, no sql injection should be possible, because java knows that what goes into the SQL statement is user input and should not be interpreted as SQL.
If you wish to know more about this,i suggest you go to Prepared statements - Java
Now, to go to your question, Regular expressin would be possible.
The String class has a .matches(String regex) function.
If you only want a-z, A-Z, 0-9 your regex will be: "[a-zA-Z0-9]*"
If you wish to know more about this,i suggest you go to Prepared statements - Java
Now, to go to your question, Regular expressin would be possible.
The String class has a .matches(String regex) function.
If you only want a-z, A-Z, 0-9 your regex will be: "[a-zA-Z0-9]*"
String regex = "[a-zA-Z0-9]*";
if( userInput.matches(regex) ){
...
} else {
System.out.println("invalid characters used");
}
#3
Posted 06 January 2011 - 09:38 AM
thank you!
now so i understand why is the * at the end of the []
[a-zA-Z0-9]*
i searched for .matches on oracle.com and i saw that space is \p{Blank}, i tried to include that too without too much success
String regex = "[a-zA-Z0-9]\\p{Blank}*";
now so i understand why is the * at the end of the []
[a-zA-Z0-9]*
i searched for .matches on oracle.com and i saw that space is \p{Blank}, i tried to include that too without too much success
String regex = "[a-zA-Z0-9]\\p{Blank}*";
#4
Posted 06 January 2011 - 09:45 AM
[a-zA-Z0-9]*
- : is a range, a-z is all letters between a-z, a and z included
a-zA-Z0-9 : Without brackets this regex would match "dL4" or "pR5"
[a-zA-Z0-9] the brackets indicate that it's 1 letter that must match everything inside it, so this regex matches "A" or "d" or "9" or "D" ...
[a-zA-Z0-9]* the asterix symbol indicates that whatever is before it can be repeated zero or more times.
The regex you made
[a-zA-Z0-9]\\p{Blank}*
matches 1 (ONE) letter that matches a-zA-Z0-9 and then zero or more spaces.
I think what you want to do, is put the space between the brackets.
Possibly a "real" space also works: [a-zA-Z0-9 ]*
(Note the space after '9'.
- : is a range, a-z is all letters between a-z, a and z included
a-zA-Z0-9 : Without brackets this regex would match "dL4" or "pR5"
[a-zA-Z0-9] the brackets indicate that it's 1 letter that must match everything inside it, so this regex matches "A" or "d" or "9" or "D" ...
[a-zA-Z0-9]* the asterix symbol indicates that whatever is before it can be repeated zero or more times.
The regex you made
[a-zA-Z0-9]\\p{Blank}*
matches 1 (ONE) letter that matches a-zA-Z0-9 and then zero or more spaces.
I think what you want to do, is put the space between the brackets.
Possibly a "real" space also works: [a-zA-Z0-9 ]*
(Note the space after '9'.
#5
Posted 06 January 2011 - 09:48 AM
THANK YOU!
i've been trying to get this working for like hours
i've been trying to get this working for like hours
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









