Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Regular Expression required!!

  • 20-08-2004 9:32am
    #1
    Moderators, Society & Culture Moderators Posts: 2,688 Mod ✭✭✭✭


    writing code to validate a new users password in ASP.NET and c#

    not used to Regex's at all to be honest, im currently checking this

    [a-zA-Z0-9]

    but it only ensures that the string contains alpha OR numeric values... can anyone enlighten me as to how to ensure a string is alphanumeric but MUST HAVE a numeric value in it.

    eg i want to ensure that all passwords are in this format....

    "password1"

    "mypass3ord"

    "password34"

    "elephant50"

    "elrond1234"

    regards
    Morph :eek:


Comments

  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro




  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro




  • Registered Users, Registered Users 2 Posts: 218 ✭✭Haruspex


    This site should provide you with a solution:
    Reg Ex Lib
    Expressions can be tested for accuracy also.


  • Closed Accounts Posts: 47 PhilH


    You want to chech this, but...

    The pattern you give [a-zA-Z0-9] will match a single character which is an letter or a number

    so this pattern matches


    a - true
    F - true
    9 - true
    a9 - false, this is two characters long, not 1
    bm - false, this is two characters long, not 1


    The pattern [a-zA-Z0-9]* will match a sequence of zero or more letter/numbers.


    a9 - true
    b_m - false, as one of the characters is not a letter/number


    I think the easiest way for you to handle your situation is to use more than one pattern. I would suggest at least three...

    [a-zA-Z0-9]* - a sequence of zero or more letters or numbers
    If the password doesn't match this it must contain illegal characters.

    [^0-9]* - a sequence of zero or more characters that are not digits
    If the password matches this then it has no digits in it.

    [^a-zA-Z]* - a sequence of zero or more characters that are not letters
    If the password matches this then it has no letteres in it (which is probably also a problem for you)

    You will probably also want to check that the password is above some minimum length.

    Hope that's of some use to you. Please check the patterns I gave you, they work for me but I don't know what regular expression engine you are using. I was just using Java, with the following code...
    Pattern p = Pattern.compile(_pattern);
    Matcher m = p.matcher(_text);
    System.out.println(m.matches());
    

    PHiL


  • Moderators, Society & Culture Moderators Posts: 2,688 Mod ✭✭✭✭Morpheus


    Wrote the following method that takes in the test string and returns true or false

    but it failed :(

    sample strnigs passed in were....

    password1 = should return true....
    password = should fail cos theres no numeric value in it
    pass2222word = should return true

    all failed :(
    
    public bool IsAlphaNumeric(string strToCheck)
    {
    		
        Regex objAlphaNumericPattern=new Regex("^(?=.*\\d).{4,8}$") ;
        return !objAlphaNumericPattern.IsMatch(strToCheck,0) ;
    }
    
    

    anyone any more ideas???


  • Advertisement
  • Closed Accounts Posts: 286 ✭✭Kev


    it failed because password1 and pass2222word are longer than 8 characters


  • Registered Users, Registered Users 2 Posts: 437 ✭✭Spunj


    Heres a quick stab at it:
    ([a-zA-z]+[0-9]+[a-zA-z]?)|([a-zA-z]+[0-9]+)|([0-9]+[a-zA-z]+\w?)
    

    you didnt mention any specific lengths of password so that will validate short ones (2 characters).


  • Moderators, Society & Culture Moderators Posts: 2,688 Mod ✭✭✭✭Morpheus


    This is f*cked up...

    heres the code im using to even just check that a string is alpha only...... [a-z]

    private void btnAlphaNumeric_Click(object sender, System.EventArgs e)
    {
      	if(IsAlphaNumeric(txtAlphaNumeric.Text) == true)
    	{
    		lblTrueFalse.Text = "True" ;
    	}
    	else
    		lblTrueFalse.Text = "False" ;
    	}
    }
    
    public bool IsAlphaNumeric(string strToCheck)
    {
    	Regex objAlphaNumericPattern=new Regex("[a-z]") ;
    	Debug.Write(objAlphaNumericPattern.ToString()) ;
    	return !objAlphaNumericPattern.IsMatch(strToCheck) ;
    }
    

    now if i type in "abcdef" im getting back false and if i type in "123456" i get true ???

    WHAT THE F*CK IS GOING ON??? ANY IDEAS???


  • Closed Accounts Posts: 47 PhilH


    Are you sure you want that exclamation mark in front of your return value?

    Surely IsMatch is giving you a true if strToCheck matches the expression [a-z]. Your are flipping this boolean value before passing it back to the caller.

    Assuming that though, your example of "abcdef" is causing IsMatch to return 'true'. This is a bit worrying because "abcdef" is not a single character in the range 'a' to 'z'. It might be that the IsMatch is actually checking whether or not strToCheck *contains* a match for your regular expression, rather than checking whether or not it *is* a match. If so, the method is appallingly named, but you'll have to check .NET documentation for details.

    PHiL


  • Moderators, Society & Culture Moderators Posts: 2,688 Mod ✭✭✭✭Morpheus


    WOW... flash of inspiration (and some more research and testing)

    IsMatch() searches for ANY single match possible, not any non matches so without the exclamation mark we would be returning any "matches".

    However im trying to catch any patterns that DONT match, therefore i need the exclamation mark, so that checking a string of "111" against the range a-z would return true because 111 IS a NON match.

    therefore my current regex of ^(?![0-9]{6})[0-9a-zA-Z]{6}$ substituted into the above code snippet returns the following...

    passs1 = false (no non matches found)

    pas1 = true (non match because its less than 6 chars long)

    pa1 = true (as above)

    111 = true (less than 6 and no alphas)

    1pass2 = false (no non matches)

    so false is true and true is false :o)

    thanks for all the help guys!


  • Advertisement
  • Closed Accounts Posts: 47 PhilH


    Looking at your regular expression, my first reaction is 'jesus, that looks complicated'. You should be able to make much simpler (and therefore very much easier to understand and maintain) code.

    You have a method that tells you is there is a match for regular expression 'x' in string 's' (terrible naming by the .NET guys, calling it IsMatch, but what can you do?).

    Do four checks, not one

    1) if there is a match for '[^0-9A-Za-z]' in 's' then 's' contains some character that is not a letter or digit, so reject it

    2) if there is no match for '[0-9]' in 's' then 's' doesn't contain a digit, so reject it

    3) if there is no match for '[A-Za-z]' in 's' then 's' doesn't contain a letter, so reject it

    4) check the length of 's' to make sure that it is long enough for your needs

    From that, I really think you're pretty damn close to having your problem solved.

    It *may* be possible to right a single regular expression that would perform the check that you need (I am not at all confident that this is the case, but maybe it's possible). However, the IsMatch function you are relying on would only go as far as telling you that 's' *contains* a valid password, not that it *is* one. So it would say '--P4ssw0rd::' is valid, because 'P4ssw0rd' is fine and is in the middle there.

    So if I were you, I would cut my losses now and not bother looking for that possible one-regular-expression test, and code up something simple like the steps above.

    PHiL


  • Moderators, Society & Culture Moderators Posts: 2,688 Mod ✭✭✭✭Morpheus


    i did it even more simple...
    
    public bool ValidatePassword(string pwd)
    {
    	int num = 0 ;
    	int chr = 0 ;
    
    	// Ensure the password is 8 or more characters in length
    	if (pwd.Length >= 8)
    	{
    		foreach(char x in pwd)
    		{
    			// check at least one character is a number
    			if(char.IsNumber(x) )
    			{
    				num++ ;
    			}
    			// check at least one character is a letter (a-z)
    			if(char.IsLetter(x) )
    			{
    				chr++ ;
    			}
    		}
    		// check if the password contains both numeric and alpha characters
    		if(chr > 0 && num > 0)
    			return true ;
    		else
    			return false ;
    	}
    
    	// password is invalid
    	else
    		return false ;
    }
    
    

    Were not worried about underscores, etc, just 8 or more chars containing letters and at least one number.

    No RegEx's :D


  • Moderators, Society & Culture Moderators Posts: 2,688 Mod ✭✭✭✭Morpheus


    ok i need a regualr expression for irish phone numbers

    to take the following form...

    area code optionally enclosed in brackets eg (041), containing 1-3 numbers
    ^\(?\d{1,3}\)?

    followed by a space, a dash or no space or dash

    ^\(?\d{1,3}\)?\s?|-

    followed by 5 - 7 numbers making the rest of the telephone number

    ^\(?\d{1,3}\)?\s?|-\d{5,7}$


    whats wrong though? i cant get it to work???

    any ideas?


  • Closed Accounts Posts: 286 ✭✭Kev


    ^\(?\d{1,3}\)?[\s-]?\d{5,7}$

    also what was wrong with ^(?=.*\d).{4,8}$ for your first problem


Advertisement