Psycho Posted February 18, 2008 Share Posted February 18, 2008 I have an email validation expression that works, but I want to see if there is a way to refine one part and/or add one additional check. Here is the expression: /^[\w\+-]+([\.]?[\w\+-])*@[a-zA-Z0-9]{2,}([\.-]?[a-zA-Z0-9]{2,})*\.[a-zA-Z]{2,4}$/ The BOLD blue section ensures the username consists of 1 or more valid characters (alpha, numeric, '_', '+', '-') The blue section allows the user to have zero or more sub elements (separated w/ a period) with the same valid characters The BOLD red section ensures the domail consists of 2 or more valid characters (alps, numeric) The red section allows the domain to have zero or more sub domains (separated w/ a period or '-') and the same valid characters [yes, the dash is not a sub-domain delimiter, but it is not acceptable at the begining or end of a domain just like the period] The BOLD green section ensures the TLD consists of 2 to 4 acceptable characters (alpha or numeric) So, my question are: 1) Can the blue sections be combined more efficiently into one such that the usrename can accept a period, but not at the beginning or end, and cannot appear in succession? 2) Can the red section be combined into one such that a period or dash cannot be at the beginning or end - and they cannot appear in succession of each other? 3) How can I add a minimum/maximum check to the entirety of the blue section and/or the (red + green sections)? I want to ensure the username is between 1-64 characters and the domain is between 5-255 characters. Currently for #3 I am using a second regex test as follows: /^(.{1,64})@(.{4,255})$/ Quote Link to comment https://forums.phpfreaks.com/topic/91772-refining-email-regex-validation/ Share on other sites More sharing options...
effigy Posted February 19, 2008 Share Posted February 19, 2008 I think it's best the way you have it, with a few improvements: Blue: [-\w+]+(?:\.[-\w+])* Red: [a-z\d]{2,}(?:[-.][a-z\d]{2,})* (with the /i modifier) I tried grouping everything into the following, but I received "Compilation failed: regular expression too large": % ^ ### User name (?: [\w+] | (?<!^|\.) \. (?![@.]) ){1,64} @ ### Domain (?: [a-z\d](?=[a-z\d]) | (?<=[a-z\d])[a-z\d] | (?<![-.]) [-.] (?![-.]) ){4,255} ### Extension \.[a-z]{2,4} $ %ix Quote Link to comment https://forums.phpfreaks.com/topic/91772-refining-email-regex-validation/#findComment-470567 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.