• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 576
  • Last Modified:

Regular expressions help - how to disallow two characters to be next to one another

email validation checker -

I have


I want to disallow joe@j-.-d.com or joe@j.-.d.com
and make this validator even better


  • 5
  • 2
  • 2
3 Solutions
BishorkAuthor Commented:
i found

online which does not allow -. and .- together but I'm not sure why or how to integrate it into my own

any help would be appreciated.  thanks!
instead of writing/copying some complicated regular expression, why not just write a simple function that accepts an array of strings to look for.. for example

the code below is untested.. but using this way, you have complete control of what character sequences you want to disallow. You can easily add, remove, modify them as needed.
function hasUnallowedSequences(array, target) {
	returnValue = false;
	for (y in array) {
		m = array[x];
		if (target.indexOf(m) != -1) {
			returnValue = true;
		} else {}
	return returnValue;
notAllowed = new Array (
		"e "
target = "some string value";
passed = hasUnallowedSequences(notAllowed, target);

Open in new window

sorry for the lack of explanation.

The function looks at all the entries in the array, and checks each one for a match within the target string. When it finds a sequence that exists in the target string, it stops looking and returns a boolean answer..

false = no matches
true = has a disallowed sequence
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

The regex,
dissallows adjacent periods, dashes, and underscores the same way your regex disallows adjacent periods

The trick is here:


It is a "look-ahead".  It says look for things in the character class (_.-) and allow them only if they are followed by one or more(+) of the second character class (a-z0-9).  The * at the end says look for zero or more of these.

Your regex:

has a look ahead in it already (the ? is the lookahead giveaway)...

I'd modify your regex to accomidate like this:  


the ([a-z0-9]|[._-](?![._-]))+ says we are looking for:

a-z0-9  or (one of "._-" as long as it is not followed by any of "._-")

I'd also like to point out the use of the + instead of the {1,} .... remember: they mean the same thing.  The entire regex could be written as:


which is quite a bit longer...
BishorkAuthor Commented:
Hey Roba,

Ok that makes sense now.

Now I have


I wanted to make it so you could not have joe@j.com, joe@j-.com, joe@joe-.com.  I used {1} to make the first two sets after the @ sign be for the first two characters you put in, then it can start accepting both periods and dashes.  

You can still pass through a jeo@e-d.e.e.e.e.com

Is there a way I can limit the number of periods allowed in the domain, before the .xxx.  I think you should only be able to have 1 right?  joe@joe.ir.com, or maybe 2 (can you have jack@john.co.ur.com

BishorkAuthor Commented:
hmm but now with what i just pasted above, test@ab.com doesn't work

BishorkAuthor Commented:
ok i guess i want to make sure i say that, you have to have 1 character from a-z 0-9 after the @ sign, then after that you can only have a period if you have at least 2 chars from a-z 0-9, but the problem i run into is that you should be able to have a hyphen after only having 1 character from a-z 0-9

joe@f-f.com is ok, joe@j.com is not ok
>  test@ab.com doesn't work.  

True.  You have it setup to require at least three characters between the @ and the .

>Is there a way I can limit the number of periods allowed in the domain, before the .xxx.  I think you should only be able to have 1 right?

Is there a way? Yes.  Should you? No.  A valid email address is: user@  Not that many people use their ip address instead of a domain name, but it is valid.

You need to change the repetition operator if you want to limit the number of periods in the domain.  Right now it is the + operator.  If you want to limit the number of periods, change it to a more specific operator.  
BishorkAuthor Commented:
I think I finally got it w/ /^[a-z0-9](([_\.\-]?[a-z0-9]+)*)@([a-z0-9])+([\-]?[a-z0-9]+)(([\.\-]?[a-z0-9]+)*)\.([a-z]{2,})$/i

now I want to create a regular expression that allows any character.
I want j$^^$j@j#$j.com to pass this test, basically allowing any character between the first and last letter in the local and domain portion.


I am trying to have it pass a form test first, then if you fail this test you'll get a message saying your email is of invalid form, then if you pass this test i'll run the more comprehensive check and tell you that you cannot have invalid characters.  (not my idea)

i posted a new question
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now