views:

61

answers:

2

Hello, i want to to validate a string if contains only a-z, 0-9, and the symbols . - _ @ and ; i have this code:

var regex = new RegExp('[a-z0-9@.;_-]{8,}');

and is not far from what i am looking for but it return true also for:

'[email protected] °%'

I'm working with jquery to load the string by a div, i put a some code more:

    var mailinglist= $('#mailinglist').val();
    var regex = new RegExp('\^[0-9a-z.-_;@]{8,}$\','i');

    if (mailinglist.match(regex)){}
    else{}

i need the match to return false if is present a blank space or any other char not defined in the pattern.

thanks :-)

+6  A: 

Add the start- and end-of-string anchors.

var regex = /^[a-z0-9@.;_-]{8,}$/
//           ↑                 ↑

Also, unless you have some dynamic pattern, prefer the regex literal /.../ over constructing a RegExp object from string new RegExp('...').

KennyTM
How many dupes on this `^…$` issue has there been now? One of these days I'd hope to be the lucky one to land one of these...
polygenelubricants
for me it doesn't work, i extend the topic with more code
TrustWeb
@TrustWeb: `var regex = /^[a-z0-9@.;_-]{8,}$/;` itself is already a valid statement! **Do not** use `new RegExp`!
KennyTM
...and the delimiters are supposed to be *forward* slashes, not backslashes.
Alan Moore
A: 

ok found here on stackoverflow, i had to search better

http://stackoverflow.com/questions/1344319/how-to-validate-input-using-javascript

TrustWeb