• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Regex help

rivan

Diamond Member
I'm a total code hack - I'll admit up front.

I've got a bit of regex to validate a form field as a valid email address (within our company) but I'd like it to validate OK if left blank as well.

Take this generic email validation:
([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{3})

How would I modify that to include an "or (null)"?
 
Could you just hack it by wrapping the entire thing in parenthesis and a *, meaning 0 or more of that block? This is just off the top of my head without testing
 
I'm a total code hack - I'll admit up front.

I've got a bit of regex to validate a form field as a valid email address (within our company) but I'd like it to validate OK if left blank as well.

Take this generic email validation:
([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{3})

How would I modify that to include an "or (null)"?

([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{3})|

I assume ^ and $ are implied?

^(([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{3})|)$

@Tencntraze, you mean a ?.

(([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{3}))?
 
([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{3})|

I assume ^ and $ are implied?

^(([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{3})|)$

@Tencntraze, you mean a ?.

(([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{3}))?

As I mentioned, I'm a complete hack - I can pick apart the bulk of how what I have functions, but not have a full understanding of what I'm reading.

I know it functions in my application without the beginning- or end-of-string flags.

So, just to make sure I'm reading what I think I am, you took what I pasted, added the pipe as 'or', then nothing to function as the null match, and wrapped the set in parens?
 
* means 0 or more while ? means 0 or 1, right?
Yes.

So, just to make sure I'm reading what I think I am, you took what I pasted, added the pipe as 'or', then nothing to function as the null match, and wrapped the set in parens?
Yes. And I added ^ and $ to make sure it matches the beginning and end of the string. Without them, and with this change, anything could appear in the string!
 
Could you just hack it by wrapping the entire thing in parenthesis and a *, meaning 0 or more of that block? This is just off the top of my head without testing

Thanks for giving it a shot - this didn't work (though I can't help but wonder if it wasn't just my CMS).

Yes. And I added ^ and $ to make sure it matches the beginning and end of the string. Without them, and with this change, anything could appear in the string!

Perfect - it's up and running - thanks for your help!
 
Back
Top