Regex help in java validations
- by user1697113
Hi i want to do some validations.I used to put regex in JS but im new to regex in java, so i tried to make up a code on similar lines in java.
Here is what i did.
1)Check whether first character in string is alphanumeric.
2)Check whether the string atleast 1 number.
so i wrote a code, but it is always returning false.I am not sure if i'm doing this correctly.
private static boolean checkEmbeddedPassword(final String field) {
boolean returnValue=true;
String testpatternAlpha="/^[A-Za-z0-9].+$/";
String testNumber="/[0-9]/";
Pattern pattern=Pattern.compile(testpatternAlpha);
Pattern pattern2=Pattern.compile(testNumber);
Matcher matcher = pattern.matcher(field);
Matcher matcher2 = pattern2.matcher(field);
boolean firstChar=matcher.matches();
boolean numberFlag=matcher2.matches();
System.out.println("-----the value of pwd iss-----"+field);
System.out.println("---------Regex---------Out--put-----"+firstChar);
System.out.println("---------Regex---------Out- for numeral-put-----"+numberFlag);
if(firstChar){
returnValue=false;
}
else if(field.contains(" "))
{
System.out.println("-----------cannot have space------");
returnValue=false;
}
else if(numberFlag)
{
returnValue=false;
}
return returnValue;
}