Jump to content

Alphanameric regular expression that requires both?


grippat

Recommended Posts

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

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

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

How about this:"^([0-9a-z]+){8,}$"or if you are looking of Hexadecimal notationis_numeric($var)do perfectly the jobhope that help
One 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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...