Click here to Skip to main content
13,145,603 members (39,559 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
I just wana check if the password contain special character or not?
 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 19-Nov-12 8:51am
Updated 19-Nov-12 9:45am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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");
         }
          
      }
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You're doing it the hard way in C anyway.
Try strchr()...
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 23 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100