grippat Posted September 12, 2007 Share Posted September 12, 2007 How would I go about creating a regular expression that searches for a string that is only numbers and letters, but has to have both? Right now I have this:"^([0-9a-z])+$"It checks for numbers and letters only, but I'm not sure how to make it require both in the string. Link to comment Share on other sites More sharing options...
grippat Posted September 12, 2007 Author Share Posted September 12, 2007 I think I may have solved it by just checking for alphanumeric with that regex then putting the string through another conditional to check for just number and just letters and return an error if either fails. But if anyone can come up with a single regex to do the same thing I'd still like to know it. Link to comment Share on other sites More sharing options...
Synook Posted September 12, 2007 Share Posted September 12, 2007 Oops that won't work. Ignore me Link to comment Share on other sites More sharing options...
justsomeguy Posted September 12, 2007 Share Posted September 12, 2007 That's pretty much the same thing, except it's 0 or more instead of 1 or more. I was thinking you could do something like this:"^([0-9a-z])*([a-z])+([0-9])+([0-9a-z])*$"That would be 0 or more alphanumeric characters, then at least one letter, then at least one number, then 0 or more alphanumeric characters. The only problem with this is that it would not match a string that had only one number followed by only one letter. It would match a single letter followed by a single number, but not the other way around. I believe you can use backreferences to get it to match one or the other, and then the other one, but regular expressions with backreferences are something I don't have a lot of experience with. You'll also want to use a modifier to make that pattern case-insensitive so it matches uppercase also. Link to comment Share on other sites More sharing options...
grippat Posted September 13, 2007 Author Share Posted September 13, 2007 Well the requirements for the string is that it must be at least 8 characters if that makes any difference. Link to comment Share on other sites More sharing options...
Matpatnik Posted September 13, 2007 Share Posted September 13, 2007 How about this:"^([0-9a-z]+){8,}$"or if you are looking of Hexadecimal notationis_numeric($var)do perfectly the jobhope that help Link to comment Share on other sites More sharing options...
justsomeguy Posted September 13, 2007 Share Posted September 13, 2007 How about this:"^([0-9a-z]+){8,}$"or if you are looking of Hexadecimal notationis_numeric($var)do perfectly the jobhope that helpOne of his requirements was that at least one letter and at least one number should appear, those don't check for that. Link to comment Share on other sites More sharing options...
Matpatnik Posted September 13, 2007 Share Posted September 13, 2007 Ok then this should do I think:"^(([a-z]+[0-9])|([0-9]+[a-z])){4,}$"it's this ((letter + number) or (number + letter)) time 4 Link to comment Share on other sites More sharing options...
justsomeguy Posted September 13, 2007 Share Posted September 13, 2007 Yeah, that looks like it would work. Nice one. Link to comment Share on other sites More sharing options...
Matpatnik Posted September 13, 2007 Share Posted September 13, 2007 Yeah, that looks like it would work. Nice one.Thank you,Happy to help Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.