0

I have following regex for email validation

/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;

only issue with this or similar solution available is that 123@domain.com passes this regex.

I do not want any email to start with a digit.

erik258
  • 14,701
  • 2
  • 25
  • 31
user1390517
  • 249
  • 2
  • 10
  • 23
  • 1
    Your regex doesn't match all valid email addresses, but you seem to want that (See https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression) – erik258 Dec 27 '21 at 17:44
  • 1
    Why are you creating a regex for email validation? There are validators built into the browser and javascript libraries to do it for you. – Yuriy Faktorovich Dec 27 '21 at 17:45
  • 1
    The only really valuable email validation is to send an email with a validation link. But I think the OP here is trying to further restrict emails. – erik258 Dec 27 '21 at 17:46
  • using the reg ex provided – user1390517 Dec 27 '21 at 17:49
  • ^[a-zA-Z0- 9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$ this was available on above link but it again fails to take care of email starting with a digit – user1390517 Dec 27 '21 at 17:50
  • /^[a-zA-Z.!#$%&'+/=?^_{|}~-][a-zA-Z0-9.!#$%&'+/=?^_{|}~-]*@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/; this one now works with most of the email, but still one issues remains it doesn;t check for domain ....it takes abc@com as valid email but instead it should check for abc@domain.com – user1390517 Jan 02 '22 at 14:06

3 Answers3

0
^[a-zA-Z0-9.!#$%&'+/=?^_`{|}~-]+@

This is the part of the regex that matches everything up to the @ separator. 0-9. matches any digit. So create a new phrase before that that doesn't match 0-9. Then change the quantifier (currently +, at least 1) to * (0 or more) so that emails with only one character work (as long as it's not a number of course).

^[a-zA-Z.!#$%&'+/=?^_`{|}~-][a-zA-Z0-9.!#$%&'+/=?^_`{|}~-]*@

See https://regex101.com/r/qjdZ0A/1 for an interactive example.

erik258
  • 14,701
  • 2
  • 25
  • 31
  • var validRegex = /^[a-zA-Z.!#$%&'+/=?^_`{|}~-][a-zA-Z0-9.!#$%&'+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/ this one works for all thanks – user1390517 Dec 27 '21 at 17:57
  • nope, that doesn't work on email addresses like `a@domain.com` because its one character before `@` can't match both the exactly-1 set and the at-least-1 set. If you want to reject single character emails as well, then yours is correct. – erik258 Dec 27 '21 at 17:58
  • /^[a-zA-Z.!#$%&'+/=?^_`{|}~-][a-zA-Z0-9.!#$%&'+/=?^_`{|}~-]*@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/; this one now works with most of the email, but still one issues remains it doesn;t check for domain ....it takes abc@com as valid email but instead it should check for abc@domain.com – user1390517 Jan 02 '22 at 14:05
0

Prepend the expression of the email with a negative lookahead...

^(?![0-9])+[a-zA-Z0-9.!#$%&'+=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)$
 _________

This only matches the email if the first character is NOT 0-9

joshwcorbett
  • 389
  • 5
  • 18
  • Why the `+` in `(?![0-9])+`? One or more identical negative lookaheads? – Cary Swoveland Dec 28 '21 at 00:14
  • /^[a-zA-Z.!#$%&'+/=?^_{|}~-][a-zA-Z0-9.!#$%&'+/=?^_{|}~-]*@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/; this one now works with most of the email, but still one issues remains it doesn;t check for domain ....it takes abc@com as valid email but instead it should check for abc@domain.com – user1390517 Jan 02 '22 at 14:06
0

reg07 is what you need.

Code includes the process into building it

const stringToTest = "zaz234@microshit.kazo"

// Ideal
const regex = new RegExp('(^[^@.]+)@([^@.]+)\.{1}(\w{1,6}$)');
const reg = /(^(.+?))@(.+?).(\w{1,6})/;

const reg00 = /(^(.+)@)/;
const reg01 = /(^[^@]+)@/;
const reg02 = /(^[^@]+)@([^@]+)/;
const reg03 = /(^[^@]+)@([^@]+)\.{1}/;
const reg04 = /(^[^@]+)@([^@]+)\.{1}(\w+)/;
const reg05 = /(^[^@]+)@([^@]+)\.{1}(\w{2,4})/;
const reg06 = /(^[^@]+)@([^@]+)\.{1}(\w{1,6}$)/;
const reg07 = /(^[^@.]+)@([^@.]+)\.{1}(\w{1,6}$)/;

console.log(`We are testing this RexEx:\n${reg07}\n\nTo see if matches this string:\n${stringToTest}\n\nResults:`)
const booleanReg = reg07.test(stringToTest) 
const arrayReg = reg07.exec(stringToTest)

console.log(`1.Simeple Match:\n${booleanReg}\n2.Array Match:\n${arrayReg}`)
const emailInfo = {
  emailUserName: arrayReg[1],
  emailProvider: arrayReg[2],
  emailExtention: arrayReg[3]
}

console.log(`\n\nemail objet build:\n\n`, emailInfo)

Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68
Matador
  • 51
  • 1
  • 6