Regex help in java validations

Posted by user1697113 on Stack Overflow See other posts from Stack Overflow or by user1697113
Published on 2012-11-23T10:58:23Z Indexed on 2012/11/23 10:59 UTC
Read the original article Hit count: 277

Filed under:
|

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;
        }

© Stack Overflow or respective owner

Related posts about java

Related posts about regex