Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 568
  • Last Modified:

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

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
Bishork
Asked:
Bishork
  • 5
  • 2
  • 2
3 Solutions
 
BishorkAuthor Commented:
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
 
MMDeveloperCommented:
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
 
MMDeveloperCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
robacarpCommented:
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
 
BishorkAuthor Commented:
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
 
BishorkAuthor Commented:
hmm but now with what i just pasted above, test@ab.com doesn't work

0
 
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
0
 
robacarpCommented:
>  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
 
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.

/^[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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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