How to match patterns in with regular expressions
i开发者_如何学C 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:
'123@123.com °%'
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 :-)
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('...')
.
ok found here on stackoverflow, i had to search better
How to validate input using javascript
精彩评论