Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I just wana check if the password contain special character or not?
C++
 char[] NewPswrd = NewPswrdField.getPassword();//this line is of java

int length=NewPswrd.length;
for(int i=0;i<length;i++)
	{
		
              char*  digits = "!@#$%^&*()_-+=";
			while(*digits)
        {
        if((NewPswrd[i]==*digits++))
        {
        }  
}
}
Posted
Updated 19-Nov-12 9:45am
v2

Java unlike C++ has regular expression support, though c++ 11 and boost library has it
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class PasswordValidator{
 
      private Pattern pattern;
          private Matcher matcher;
 
          private  final String PASSWORD_PATTERN = 
              "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";
 
          public PasswordValidator(){
                  pattern = Pattern.compile(PASSWORD_PATTERN);
          }
 
          /**
           * Validate password with regular expression
           * @param password password for validation
           * @return true valid password, false invalid password
           */
          public  boolean validate(String password){
 
                  matcher = pattern.matcher(password);
                  return matcher.matches();
 
          }
      public static void main(String arg[]) {
          PasswordValidator pv = new PasswordValidator();
          // test 1
          String pw = "dharmac1A@";
          if(pv.validate (pw)) {
              System.out.println("Valid");
          } else {
              System.out.println("InValid");
          }
          
       // test 2
           pw = "iamnotvalidpassword";
          if(pv.validate (pw)) {
              System.out.println("Valid");
          } else {
              System.out.println("InValid");
          }
          
       // test 3
          pw = "dharmatejachalla";
         if(pv.validate (pw)) {
             System.out.println("Valid");
         } else {
             System.out.println("InValid");
         }
          
      }
}
 
Share this answer
 
v2
You're doing it the hard way in C anyway.
Try strchr()...
 
Share this answer
 
Comments
Tarun Batra 20-Nov-12 0:07am    
sir can you give me the pseudo code in java i have to do the above in java
JackDingler 20-Nov-12 8:43am    
The table I looked it up in, and don't have it handy now, explained that strchr() is available in both Java and C++.

I'm not a Java programmer, though I've debugged quite a bit of Java code.

Do you know how to do a google search on strchr? If this isn't a homework assignment, it's the better way to do this.

If this is a homework assignment, then you should consult with your instructor, mentor or teacher's aid for help on this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900