Solved

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

Posted on 2008-10-13
9
526 Views
Last Modified: 2012-05-05
email validation checker -

I have

/^[a-z0-9]{1,}([a-z0-9_-]|\.(?!\.))+@[a-z0-9]{1,}([a-z0-9]|\.(?!\.)|\-(?!\-)){1,}[a-z0-9]+\.[a-z0-9]{2,6}$/i;

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

thanks

0
Comment
Question by:Bishork
  • 5
  • 2
  • 2
9 Comments
 

Author Comment

by:Bishork
ID: 22704384
i found
^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$

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!
0
 
LVL 15

Assisted Solution

by:MMDeveloper
MMDeveloper earned 150 total points
ID: 22704836
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;

			break;

		} else {}

	}

	

	return returnValue;

}
 
 
 
 

notAllowed = new Array (

		".-",

		"-.",

		"e "

	);

target = "some string value";
 

passed = hasUnallowedSequences(notAllowed, target);

Open in new window

0
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22704847
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
0
 
LVL 3

Accepted Solution

by:
robacarp earned 350 total points
ID: 22705137
The regex,
^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$
dissallows adjacent periods, dashes, and underscores the same way your regex disallows adjacent periods

The trick is here:

(([_\.\-]?[a-zA-Z0-9]+)*)

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:
/^[a-z0-9]{1,}([a-z0-9_-]|\.(?!\.))+@[a-z0-9]{1,}([a-z0-9]|\.(?!\.)|\-(?!\-)){1,}[a-z0-9]+\.[a-z0-9]{2,6}$/i

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

I'd modify your regex to accomidate like this:  

/^[a-z0-9]+([a-z0-9]|[._-](?![._-]))+@[a-z0-9]+([a-z0-9]|[._-](?![._-]))+.[a-z0-9]{2,6}$/i

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:

/^[a-z0-9]{1,}([a-z0-9]|[._-](?![._-])){1,}@[a-z0-9]{1,}([a-z0-9]|[._-](?![._-])){1,}.[a-z0-9]{2,6}$/i

which is quite a bit longer...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Bishork
ID: 22712155
Hey Roba,

Ok that makes sense now.

Now I have

/^[a-z0-9]+([a-z0-9]|[._-](?![._-]))+@[a-z0-9]{1}([a-z0-9]|[-](?![.-])){1}([a-z0-9]|[.-](?![.-]))+\.[a-z0-9]{2,6}$/i

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

Thanks
0
 

Author Comment

by:Bishork
ID: 22712180
hmm but now with what i just pasted above, test@ab.com doesn't work

0
 

Author Comment

by:Bishork
ID: 22712420
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
0
 
LVL 3

Assisted Solution

by:robacarp
robacarp earned 350 total points
ID: 22714501
>  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@209.85.171.99  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.  
0
 

Author Closing Comment

by:Bishork
ID: 31407045
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.

/^[a-z0-9]+???????[a-z0-9]+@[a-z0-9]+?????.([a-z]{2,})

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
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now