Basically i am trying to restrict a user not to input characters that are not allowed in username box. i found that this could be implement by String.matches()
String UcharSet = "[a-zA-Z0-9-~!##().]+";
boolean UMORN = "Username.is#example.com".matches(UcharSet);
if(UMORN != true)
UNotAllowedCharEC = "0x00000030";
as you can see i have string of characters to be allowed in my username box but somehow when i input # it return false although i have it in my allowed string list.
and do tell should i add any other characters to be allowed for my username box.
I just tested this and '#' results in true. You problem probably lies elsewhere.
Related
Ok,i am developing spring MVC based web application, application shows data is list, and i also facilitate filter options to enhanced search functionality, I also remove extra space by using trim(), but what happening now, when user input data in text field and enter the corresponding result will be displayed into the list, but if space added after input, the result will be "NOT FOUND" even i handle the space in javascript too
Java Code which fetches data from database
if (searchParamDTO.getRegNO().trim() != null && !searchParamDTO.getRegNO().trim().equals("") && !searchParamDTO.getRegNO().trim().equals("null")) {
query += " AND UR.REG_UNIQUE_ID = :REG_UNIQUE_ID ";
param.addValue("REG_UNIQUE_ID", searchParamDTO.getRegNO());
}
JavaScript Code: fetches the value in behalf of id
function setSearchParameters() {
regNo = $('#regNo').val().trim();}
i also attached two screenshot with spaces and without spaces
Without space
With space
As #Greg H said you're trimming the string when checking if it's blank, but then adding the raw string to the query which will include any trailing spaces.
Then, this line param.addValue("REG_UNIQUE_ID", searchParamDTO.getRegNO()); should be replaced by param.addValue("REG_UNIQUE_ID", searchParamDTO.getRegNO().trim());
The user input:
String usersearch = UserSearchField.getText();
I want searchFilter to take the variable usersearch
String searchFilter ="(&(samAccountName=usersearch))"
But can not figure out exactly how to do it.
Your search filter is literally searching for an account name "usersearch". You need to format your string to correctly create the search filter. Try this:
String searchFilter = String.format("samAccountName=%s", usersearch);
This code will replace the %s with the value of usersearch. Additionally you don't need the (& because that's for when you have more than one filter and you need to apply a boolean AND operator to them. See the JNDI search filter docs for more information.
I want to to input this link in to the string.
String url=www.test.com;
String link=<a href=url>contact info</a>
How can I write this ?
You will need to do:
String url = "www.test.com";
You can use \ character to indicate that we want to include a special character, and that the next character should be treated differently. \" indicates a double quote character and not the termination of the string.
String link = "contact info";
A character preceded by a backslash is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:
Java Escape Sequences:
For More information check this link
First, let's assume you have:
String url = "www.test.com";
(Note the quotes around the string.)
To create your link string, you'd do this:
String link = "contact info";
// Note ---------------^^-----------^^
To put a " inside a string literal, you put a backslash in front of it. This is called "escaping" the quote.
First have the url value within quotes ,then concat the value in the link string.
String url="www.test.com";
String link="contact info";
We have a tokenizer which tokenizes a text file .The logic followed is quite weird but necessary in our context.
An email such as
xyz.zyx#gmail.com
will result in the following tokens :
xyz
.
zyx
#
gmail
I would like to know how can we recognize the field as email if we are allowed to use only these tokens. No regex is allowed. We are allowed only to use the tokens and their surrounding tokens to figure out if the field is an email field
ok.. try some (bad) logic like this...
int i=0,j=0;
if(str.contains(".") && str.contains("#"))
{
if((i=str.indexOf(".") < (j=str.indexOf("#"))
{
if(i!=0 && i+1!=j) //ignore Strings like .# , abc.#
return true;
}
}
return false
Logically split an e-mail address into 3 parts:
A user name (or resource name), for this explanation let's call it the user name
The # character.
A host name, consisting of any number of "word dot" sequences + a final top level domain string.
Do a walk like this:
while token can be part of a user name
fetch next token;
if there no more -> no e-mail;
check if the next token is #
if not -> no e-mail
while there are tokens
while token can be part of a host name subpart (the "word" above)
fetch next token;
if there are no more -> might be a valid e-mail address
check if the next token is a dot
if not -> might be a valid e-mail address
set a flag that you found at least one dot
check if the next token can be part of a host name subpart
if not -> no valid e-mail address (or maybe you ignore a trailing dot and take what was found so far)
Add further checks if there are more tokens where needed. You also may have to post process the found tokens to ensure a valid e-mail address and you may have to rewind your tokenizer (or cache the fetched tokens) in case you did not find a valid e-mail address and need to feed the same input to some other recognition process.
Check if a list of tokens is an email:
list contains exactly one token #
index of token # != 0
at least 3 tokens after #
at least 1 . token after #, but not immediately after
starts and ends with character tokens
Additional checks:
no two . subsequent tokens
no special characters
length of character tokens after # is at least 2
total length of all character tokens before # is at least 3
I use java and a regexp.
I've made a regexp for password validation :
String PASSWORD_PATTERN_ADVANCED = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\\\\##$¤£µ§%&<>,.!:?;~{-|`'_^¨éèçàù)=}()°\"\\]\\[²³*/+]).{8,20}$";
or without the extra slash :
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\\##$¤£µ§%&<>,.!:?;~{-|`'_^¨éèçàù)=}()°"\]\[²³*/+]).{8,20}$
whuch means (i may be wrong): at least one digit / at least one lowercase / at least one uppercase / at least one of the special chars listed / with a minimum total length of 8 and a max of 20...
made a test case generating password for success and failure...
success -> OK, all passed
failure -> Almost OK ...
The only password that fails to fail :D are the ones with space in it like :
iF\ !h6 2A3|Gm
¨I O7 gZ2%L£k vd~39
2< A Uw a7kEw6,6S^
cC2c5N#
6L kIw~ Béj7]5
ynRZ #44ç
9A `sè53Laj A
s²R[µ3 9UrR q8n
I am puzzled.
Any thoughts to make it works ?
Thanks
A regex may not be the right tool for the job here.
Regexes are best suited for matching patterns; what you're describing isn't really a pattern, per se; it's more of a rule set. Sure, you may be able to create some regex that helps, but it's a really complex and opaque piece of code which make maintenance a challenge.
A method like this might be a better fit:
public boolean isValidPassword(String password) {
boolean containsLowerCase;
boolean containsUpperCase;
boolean containsInvalid;
boolean containsSpecialChar;
boolean containsDigit;
for(char c: password.toCharArray()) {
containsLowerCase ||= Character.isLowerCase(c);
containsUpperCase ||= Character.isUpperCase(c);
containsDigit ||= Character.isDigit(c);
containsSpecialChar ||= someMethodForDetectingIfItIsSpecial(c);
}
return containsLowerCase &&
containsUpperCase &&
containsSpecialChar &&
containsDigit &&
!containsInvalid &&
password.length >=8 && password.length <=20;
}
You'd need to decide the best way to detect a special character (specialCharArray.contains(c), regular expression, etc).
However, this approach would make adding new rules a lot simpler.
I may be wrong but if you simply don't want spaces then use [^\\s] instead of . in your lookahead.
String PASSWORD_PATTERN_ADVANCED =
"^(?=[^\\s]*\\d)"
+ "(?=[^\\s]*[a-z])"
+ "(?=[^\\s]*[A-Z])"
+ "(?=[^\\s]*[\\\\##$¤£µ§%&<>,.!:?;~{-|`'_^¨éèçàù)=}()°\"\\]\\[²³*/+])"
+ ".{8,20}$";
None of your conditions are stating what can't be in the password, only what must. You need one more condition that combines all the possible valid characters and makes sure all characters in the password are in that list (i.e., (\d|[a-z]|[A-Z]|##$...){8,20} as the final condition). Either that or a list of rejected characters.